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.1.0+1 |
---|---|
license | CC0-1.0 UniversalCreative Commons Zero v1.0 Universal |
repository | https://pkg.cppget.org/1/stable |
download | tl-optional-1.1.0+1.tar.gz |
sha256 | d272e8b790ac502baee6f6901fb510cd779d144bfdb44de25ffc27ae6e7f35c8 |
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.1.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:31:40 UTC (01:37:15 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:31:08 UTC (01:37:47 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:59 UTC (01:37:56 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:30:09 UTC (01:38:46 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:30:07 UTC (01:38:48 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:29:17 UTC (01:39:38 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:28:10 UTC (01:40:45 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:17:46 UTC (01:51:09 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:16:20 UTC (01:52: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:16:06 UTC (01:52:49 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:13:54 UTC (01:55:01 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:51 UTC (01:57:03 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:10:32 UTC (01:58:23 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:42 UTC (07:48:13 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:20:58 UTC (22:47:57 hours ago) |
result | success | log | rebuild |
toolchain | public-0.17.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_17_libc++ |
timestamp | 2025-09-09 11:27:00 UTC (01 02:41: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_17 |
timestamp | 2025-09-09 11:26:43 UTC (01 02:42:12 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-ndebug_O3 |
timestamp | 2025-09-09 03:01:03 UTC (01 11:07:52 days ago) |
result | success | 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:54:37 UTC (01 11:14:17 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:35:46 UTC (01 11:33:09 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:08:09 UTC (01 12:00:46 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:32:20 UTC (01 13:36: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-static_O2 |
timestamp | 2025-09-09 00:30:50 UTC (01 13:38:05 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:55 UTC (01 13:39:00 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:26:28 UTC (01 13:42:27 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:23:11 UTC (01 13:45:44 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:37:18 UTC (01 14:31:37 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:37:56 UTC (01 17:30:59 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:20:29 UTC (01 17:48:26 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:19:53 UTC (01 17:49:02 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:14:07 UTC (01 17:54:48 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:40:28 UTC (01 22:28:27 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:34:58 UTC (01 22:33:57 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:33:31 UTC (01 22:35:24 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:33:10 UTC (01 22:35:45 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:29:44 UTC (01 22:39:11 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:29:03 UTC (01 22:39:52 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:14:24 UTC (01 22:54:31 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:07:54 UTC (01 23:01:01 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:06:16 UTC (01 23:02:39 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 |