tl-expected-tests/1.0.0

[brief]

C++11/14/17 std::optional with functional-style extensions and reference support

Single header implementation of std::expected with functional-style extensions.

Documentation Status Clang + GCC: Linux Build Status MSVC: Windows Build Status

Available on Vcpkg and Conan.

std::expected is proposed as the preferred way to represent object which will either have an expected value, or an unexpected value giving information about why something failed. Unfortunately, chaining together many computations which may fail can be verbose, as error-checking code will be mixed in with the actual programming logic. This implementation provides a number of utilities to make coding with expected cleaner.

For example, instead of writing this code:

std::expected<image,fail_reason> get_cute_cat (const image& img) {
    auto cropped = crop_to_cat(img);
    if (!cropped) {
      return cropped;
    }

    auto with_tie = add_bow_tie(*cropped);
    if (!with_tie) {
      return with_tie;
    }

    auto with_sparkles = make_eyes_sparkle(*with_tie);
    if (!with_sparkles) {
       return with_sparkles;
    }

    return add_rainbow(make_smaller(*with_sparkles));
}

You can do this:

tl::expected<image,fail_reason> 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::expected as proposed in p0323r3, but the following member functions are also defined. Explicit types are for clarity.

Compiler support

Tested on:

Acknowledgements

Thanks to Kévin Alexandre Boissonneault and Björn Fahller for various bug fixes.


CC0

To the extent possible under law, Sy Brand has waived all copyright and related or neighboring rights to the expected 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-expected-tests-1.0.0.tar.gz
sha256 e6b1dad1749d0c68228f4a0f5e2eeaf57c4c536cb97f5e3b223645aee8ae23c6
project tl
doc-url tl.tartanllama.xyz/en/latest/
package-url github.com/build2-packaging/build2-tl
package-email wmbat-dev@protonmail.com

Depends (1)

catch2 ^2.13.7

Requires (1)

c++ >= 11

Reviews

fail 0
pass 1