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 |
Builds
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_15.0_libc++ |
timestamp | 2024-05-01 10:47:51 UTC (17:11 minutes ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_15.0 |
timestamp | 2024-05-01 10:45:04 UTC (19:58 minutes ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_38-gcc_13-bindist |
timestamp | 2024-05-01 09:49:29 UTC (01:15:33 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_fedora_37-gcc_12.2-bindist |
timestamp | 2024-05-01 04:01:45 UTC (07:03:17 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64 |
timestamp | 2024-05-01 03:23:16 UTC (07:41:46 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64-static_O2 |
timestamp | 2024-05-01 03:17:47 UTC (07:47:15 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-w64-mingw32 |
tgt config | windows_10-gcc_12.2_mingw_w64-O2 |
timestamp | 2024-05-01 03:15:55 UTC (07:49:07 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_12-bindist |
timestamp | 2024-05-01 03:12:30 UTC (07:52:32 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-static_O3 |
timestamp | 2024-05-01 03:09:04 UTC (07:55:58 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-ndebug_O3 |
timestamp | 2024-05-01 03:07:50 UTC (07:57:12 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1-O3 |
timestamp | 2024-05-01 03:06:24 UTC (07:58:37 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-gcc_13.1 |
timestamp | 2024-05-01 03:03:05 UTC (08:01:57 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_11-gcc_12.1 |
timestamp | 2024-05-01 03:02:04 UTC (08:02:58 hours ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6-static_O2 |
timestamp | 2024-04-30 15:02:05 UTC (20:02:57 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6-O2 |
timestamp | 2024-04-30 15:01:26 UTC (20:03:36 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_16.0_llvm_msvc_17.6 |
timestamp | 2024-04-30 15:00:39 UTC (20:04:23 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++-static_O3 |
timestamp | 2024-04-30 14:55:35 UTC (20:09:27 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0-O3 |
timestamp | 2024-04-30 14:54:41 UTC (20:10:21 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++-O3 |
timestamp | 2024-04-30 14:53:41 UTC (20:11:21 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0-static_O3 |
timestamp | 2024-04-30 14:53:31 UTC (20:11:31 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0_libc++ |
timestamp | 2024-04-30 14:51:35 UTC (20:13:27 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-linux-gnu |
tgt config | linux_debian_12-clang_16.0 |
timestamp | 2024-04-30 14:51:13 UTC (20:13:49 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0 |
timestamp | 2024-04-30 12:07:36 UTC (22:57:26 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6-static_O2 |
timestamp | 2024-04-30 11:57:48 UTC (23:07:14 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6-O2 |
timestamp | 2024-04-30 11:55:33 UTC (23:09:29 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.6 |
timestamp | 2024-04-30 11:53:36 UTC (23:11:26 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0-static_O3 |
timestamp | 2024-04-30 11:52:37 UTC (23:12:24 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd13.2 |
tgt config | freebsd_13-clang_14.0-O3 |
timestamp | 2024-04-30 11:52:14 UTC (23:12:48 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-clang_15.0_msvc_msvc_17.6 |
timestamp | 2024-04-30 11:48:00 UTC (23:17:02 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-microsoft-win32-msvc14.3 |
tgt config | windows_10-msvc_17.5 |
timestamp | 2024-04-30 11:35:57 UTC (23:29:05 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-freebsd12.4 |
tgt config | freebsd_12-clang_13.0 |
timestamp | 2024-04-30 11:29:02 UTC (23:36:00 hours ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0-static_O3 |
timestamp | 2024-04-30 08:40:08 UTC (01 02:24:54 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0-O3 |
timestamp | 2024-04-30 08:38:35 UTC (01 02:26:27 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_14.0 |
timestamp | 2024-04-30 08:34:45 UTC (01 02:30:17 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-static_O3 |
timestamp | 2024-04-30 06:35:25 UTC (01 04:29:37 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-O3 |
timestamp | 2024-04-30 06:29:49 UTC (01 04:35:12 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13-ndebug_O3 |
timestamp | 2024-04-30 06:26:46 UTC (01 04:38:16 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-gcc_13 |
timestamp | 2024-04-30 06:22:14 UTC (01 04:42:48 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0-static_O3 |
timestamp | 2024-04-30 01:25:53 UTC (01 09:39:09 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0-O3 |
timestamp | 2024-04-30 01:24:39 UTC (01 09:40:23 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-clang_15.0 |
timestamp | 2024-04-30 01:18:02 UTC (01 09:47:00 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++-static_O3 |
timestamp | 2024-04-29 16:16:52 UTC (01 18:48:10 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++-O3 |
timestamp | 2024-04-29 16:13:55 UTC (01 18:51:07 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16_libc++ |
timestamp | 2024-04-29 16:12:36 UTC (01 18:52:26 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16-O3 |
timestamp | 2024-04-29 16:11:28 UTC (01 18:53:34 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16-static_O3 |
timestamp | 2024-04-29 16:10:49 UTC (01 18:54:13 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_16 |
timestamp | 2024-04-29 16:08:18 UTC (01 18:56:44 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_11-gcc_12 |
timestamp | 2024-04-29 13:50:43 UTC (01 21:14:19 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_15_libc++ |
timestamp | 2024-04-29 12:16:08 UTC (01 22:48:54 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | aarch64-linux-gnu |
tgt config | linux_debian_12-clang_15 |
timestamp | 2024-04-29 12:13:49 UTC (01 22:51:13 days ago) |
result | warning (test) | warning (test-installed) | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew-static_O3 |
timestamp | 2024-04-28 13:53:52 UTC (02 21:11:10 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew-O3 |
timestamp | 2024-04-28 13:53:11 UTC (02 21:11:51 days ago) |
result | success | log | rebuild |
toolchain | public-0.16.0 |
---|---|
target | x86_64-apple-darwin22.5.0 |
tgt config | macos_13-gcc_13_homebrew |
timestamp | 2024-04-28 13:52:29 UTC (02 21:12:33 days ago) |
result | success | log | rebuild |