C++11/14/17 std::optional with functional-style extensions and reference support
Single header implementation of std::optional
with functional-style extensions and support for references.
std::optional
is the preferred way to represent an object which may or may not have a value. Unfortunately, chaining together many computations which may or may not produce a value can be verbose, as empty-checking code will be mixed in with the actual programming logic. This implementation provides a number of utilities to make coding with optional
cleaner.
For example, instead of writing this code:
std::optional<image> get_cute_cat (const image& img) {
auto cropped = crop_to_cat(img);
if (!cropped) {
return std::nullopt;
}
auto with_tie = add_bow_tie(*cropped);
if (!with_tie) {
return std::nullopt;
}
auto with_sparkles = make_eyes_sparkle(*with_tie);
if (!with_sparkles) {
return std::nullopt;
}
return add_rainbow(make_smaller(*with_sparkles));
}
You can do this:
tl::optional<image> get_cute_cat (const image& img) {
return crop_to_cat(img)
.and_then(add_bow_tie)
.and_then(make_eyes_sparkle)
.map(make_smaller)
.map(add_rainbow);
}
The interface is the same as std::optional
, but the following member functions are also defined. Explicit types are for clarity.
map
: carries out some operation on the stored object if there is one.tl::optional<std::size_t> s = opt_string.map(&std::string::size);
and_then
: likemap
, but for operations which return atl::optional
.tl::optional<int> stoi (const std::string& s);
tl::optional<int> i = opt_string.and_then(stoi);
or_else
: calls some function if there is no value stored.opt.or_else([] { throw std::runtime_error{"oh no"}; });
map_or
: carries out amap
if there is a value, otherwise returns a default value.tl::optional<std::size_t> s = opt_string.map_or(&std::string::size, 0);
map_or_else
: carries out amap
if there is a value, otherwise returns the result of a given default function.std::size_t get_default();
tl::optional<std::size_t> s = opt_string.map_or_else(&std::string::size, get_default);
conjunction
: returns the argument if a value is stored in the optional, otherwise an empty optional.tl::make_optional(42).conjunction(13); //13
tl::optional<int>{}.conjunction(13); //empty
disjunction
: returns the argument if the optional is empty, otherwise the current value.tl::make_optional(42).disjunction(13); //42
tl::optional<int>{}.disjunction(13); //13
take
: returns the current value, leaving the optional empty.opt_string.take().map(&std::string::size); //opt_string now empty;
In addition to those member functions, optional references are also supported:
int i = 42;
tl::optional<int&> o = i;
*o == 42; //true
i = 12;
*o == 12; //true
&*o == &i; //true
Assignment has rebind semantics rather than assign-through semantics:
int j = 8;
o = j;
&*o == &j; //true
Compiler support
Tested on:
- Linux
- clang 6.0.1
- clang 5.0.2
- clang 4.0.1
- clang 3.9
- clang 3.8
- clang 3.7
- clang 3.6
- clang 3.5
- g++ 8.0.1
- g++ 7.3
- g++ 6.4
- g++ 5.5
- g++ 4.9
- g++ 4.8
- Windows
- MSVC 2015
- MSVC 2017
Standards Proposal
This library also serves as an implementation of WG21 standards paper P0798R0: Monadic operations for std::optional. This paper proposes adding map
, and_then
, and or_else
to std::optional
.
To the extent possible under law, Sy Brand has waived all copyright and related or neighboring rights to the optional
library. This work is published from: United Kingdom.
version | 1.0.0 |
---|---|
license | CC0 1.0 UniversalCreative Commons Zero v1.0 Universal |
repository | https://pkg.cppget.org/1/stable |
download | tl-optional-1.0.0.tar.gz |
sha256 | c53516bd333e81669e59aeff2f086a5d1fd8a9f36f3db07b18383dee985f1d05 |
project | tl |
---|---|
doc-url | tl.tartanllama.xyz/en/latest/ |
package-url | github.com/build2-packaging/build2-tl |
package-email | wmbat-dev@protonmail.com |
Requires (1)
c++ >= 11 |
Tests
tl-optional-tests == 1.0.0 |
Reviews
fail | 0 |
---|---|
pass | 1 |
Builds
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_17_msvc_msvc_17.10 |
timestamp | 2025-09-10 12:30:54 UTC (01:32:49 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-static_O2 |
timestamp | 2025-09-10 12:30:16 UTC (01:33:27 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10-O2 |
timestamp | 2025-09-10 12:29:29 UTC (01:34:14 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-static_O2 |
timestamp | 2025-09-10 12:29:06 UTC (01:34:37 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8-O2 |
timestamp | 2025-09-10 12:27:35 UTC (01:36:08 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.10 |
timestamp | 2025-09-10 12:27:23 UTC (01:36:20 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.8 |
timestamp | 2025-09-10 12:26:19 UTC (01:37:24 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-static_O3 |
timestamp | 2025-09-10 12:15:43 UTC (01:48:00 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd13.3 |
tgt config | freebsd_13-clang_17 |
timestamp | 2025-09-10 12:15:08 UTC (01:48:35 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18-O3 |
timestamp | 2025-09-10 12:13:40 UTC (01:50:03 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-freebsd14.1 |
tgt config | freebsd_14-clang_18 |
timestamp | 2025-09-10 12:11:43 UTC (01:52:00 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-09-10 12:11:13 UTC (01:52:30 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-09-10 12:09:58 UTC (01:53:45 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_40-gcc_14-bindist |
timestamp | 2025-09-10 06:20:00 UTC (07:43:43 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13 |
timestamp | 2025-09-09 15:21:33 UTC (22:42:10 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17 |
timestamp | 2025-09-09 11:27:13 UTC (01 02:36:30 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-09-09 11:26:28 UTC (01 02:37:15 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-09-09 02:23:21 UTC (01 11:40:23 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-09-09 02:16:41 UTC (01 11:47:02 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-09-09 02:11:22 UTC (01 11:52:21 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-09-09 02:09:16 UTC (01 11:54:28 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_12-bindist |
timestamp | 2025-09-09 00:29:28 UTC (01 13:34:15 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_ubuntu_24.04-gcc_13-bindist |
timestamp | 2025-09-09 00:25:53 UTC (01 13:37:50 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-static_O2 |
timestamp | 2025-09-09 00:25:45 UTC (01 13:37:58 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64-O2 |
timestamp | 2025-09-09 00:24:08 UTC (01 13:39:35 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_13.2_mingw_w64 |
timestamp | 2025-09-09 00:22:11 UTC (01 13:41:32 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1 |
timestamp | 2025-09-08 23:36:09 UTC (01 14:27:34 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-static_O3 |
timestamp | 2025-09-08 20:21:34 UTC (01 17:42:09 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-O3 |
timestamp | 2025-09-08 20:15:11 UTC (01 17:48:32 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14-ndebug_O3 |
timestamp | 2025-09-08 20:14:39 UTC (01 17:49:04 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_14 |
timestamp | 2025-09-08 20:12:52 UTC (01 17:50:51 days ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
timestamp | 2025-09-08 15:31:43 UTC (01 22:32:00 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
timestamp | 2025-09-08 15:27:49 UTC (01 22:35:55 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
timestamp | 2025-09-08 15:26:24 UTC (01 22:37:19 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
timestamp | 2025-09-08 15:24:57 UTC (01 22:38:46 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
timestamp | 2025-09-08 15:23:39 UTC (01 22:40:04 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
timestamp | 2025-09-08 15:18:54 UTC (01 22:44:49 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-static_O2 |
timestamp | 2025-09-08 15:11:42 UTC (01 22:52:02 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10-O2 |
timestamp | 2025-09-08 15:09:27 UTC (01 22:54:16 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_18_llvm_msvc_17.10 |
timestamp | 2025-09-08 15:04:47 UTC (01 22:58:56 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++ |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_18_libc++-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_39-gcc_13-bindist |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-clang_15.0-static_O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-O3 |
result | unbuilt |
toolchain | public-0.17.0 |
---|---|
target | x86_64-apple-darwin23.5.0 |
tgt config | macos_14-gcc_14_homebrew-static_O3 |
result | unbuilt |