+
Skip to content

ibmibmibm/expected

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expected-17

Single header implementation of std::expected in c++17 with functional-style extensions. Inspired from tl::expected

Clang + GCC: Linux Build Status

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:

std::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 p0323r9, 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.
    • std::expected<std::size_t,std::error_code> s = exp_string.map(&std::string::size);
  • map_error: carries out some operation on the unexpected object if there is one.
    • my_error_code translate_error (std::error_code);
    • std::expected<int,my_error_code> s = exp_int.map_error(translate_error);
  • and_then: like map, but for operations which return a tl::expected.
    • std::expected<ast, fail_reason> parse (const std::string& s);
    • std::expected<ast, fail_reason> exp_ast = exp_string.and_then(parse);
  • or_else: calls some function if there is no value stored.
    • exp.or_else([] { throw std::runtime_error{"oh no"}; });

Compiler support

Tested on:

  • Linux
    • g++ 9
    • g++ 8
    • g++ 7
    • clang++ 9
    • clang++ 8
    • clang++ 7

Dependencies

Requires Catch for testing. This is bundled in the test directory.

Acknowledgements

If you're looking c++11 or c++14 version, please visit tl::expected.


CC0

To the extent possible under law, Shen-Ta Hsieh has waived all copyright and related or neighboring rights to the expected library.

About

C++17 std::expected with functional-style extensions inspired from https://github.com/TartanLlama/expected

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载