• Stars
    star
    1,307
  • Rank 34,706 (Top 0.8 %)
  • Language
    C++
  • License
    Creative Commons ...
  • Created over 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C++11/14/17 std::expected with functional-style extensions

expected

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.

  • map: carries out some operation on the stored object if there is one.
    • tl::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);
    • tl::expected<int,my_error_code> s = exp_int.map_error(translate_error);
  • and_then: like map, but for operations which return a tl::expected.
    • tl::expected<ast, fail_reason> parse (const std::string& s);
    • tl::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"}; });

p0323r3 specifies calling .error() on an expected value, or using the * or -> operators on an unexpected value, to be undefined behaviour. In this implementation it causes an assertion failure. The implementation of assertions can be overridden by defining the macro TL_ASSERT(boolean_condition) before #including <tl/expected.hpp>; by default, assert(boolean_condition) from the <cassert> header is used. Note that correct code would not rely on these assertions.

Compiler support

Tested on:

  • Linux
    • clang++ 3.5, 3.6, 3.7, 3.8, 3.9, 4, 5, 6, 7, 8, 9, 10, 11
    • g++ 4.8, 4.9, 5.5, 6.4, 7.5, 8, 9, 10
  • Windows
    • MSVC 2015, 2017, 2019, 2022

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.

More Repositories

1

optional

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

minidbg

A mini x86 linux debugger for teaching purposes
C++
507
star
3

vizh

An esoteric visual language that takes image files as input based on a multi-tape turing machine, designed for compatibility with C.
Python
229
star
4

function_ref

A lightweight, non-owning reference to a callable.
C++
168
star
5

typeclasses

Future C++ implementation of Rust-like trait objects with no boilerplate
C++
116
star
6

cpp-documentation-example

An example of setting up Sphinx for C++ and building with CMake and Read the Docs
CMake
85
star
7

actions-eleventy

GitHub Action for generating a static website with Eleventy
Shell
84
star
8

non-binary

๐Ÿ’›โ™กโ™ฅ๐Ÿ’œ
Isabelle
76
star
9

ranges

Ranges that didn't make C++20
C++
73
star
10

generator

Single-header, ranges-compatible generator type built on C++20 coroutines
C++
57
star
11

tl

A bunch of small C++ utilities
C++
54
star
12

caginator

TypeScript
43
star
13

secret-hitler-strategies

Unofficial Secret Hitler strategy guide
HTML
33
star
14

etkf

Embarrassingly templated keyboard framework
C
32
star
15

enjamb

An esoteric programming language where it's not what's in your lines that matters โ€” it's where you break them.
C++
29
star
16

dltrace

A demonstration of tracing dynamic library loading and unloading on Linux.
C++
15
star
17

tartanllama.github.io

CSS
15
star
18

piscrn

A screenshot server, library, and command-line utility for the Raspberry Pi
C
12
star
19

advent-of-code-2020

Rust
11
star
20

lameduck

Terrible Brainfuck to x64 compiler developed live in an hour
C++
11
star
21

aoc-2021

C++
7
star
22

writing-a-linux-debugger

7
star
23

icu-emscripten

Binaries and headers for ICU built for Emscripten
C
7
star
24

aoc-2022

C++
5
star
25

koura

A C++ text template engine.
C++
5
star
26

cppquiz17

Porting questions on cppquiz.org to C++17
C++
4
star
27

rustcpp

Rust
4
star
28

tl-docs

Documentation for the tl libraries
Python
4
star
29

jork

C++
4
star
30

li1I

A domain-specific programming language for integer calculations. Includes LLVM-based compiler.
C++
4
star
31

cppcon-cmake

CMake
3
star
32

cpppaperbot

Python
3
star
33

amer

A C++17 static website generator
CSS
3
star
34

tl-cmake

CMake
3
star
35

advent-of-code-2018

C++
3
star
36

retroman

LaTeX template for writing retro-style technical manuals
TeX
3
star
37

pet_bee

Lua
3
star
38

raytracer

Simple C++ raytracer
C
2
star
39

secrethitler.tv

Secret Hitler on your TV!
JavaScript
2
star
40

sybrand.ink

HTML
2
star
41

shotts

C++
2
star
42

codeart

Sass
2
star
43

elmscrew

An online brainfuck debugger
Elm
2
star
44

elfheadedit

Small ELF file header editor
C++
2
star
45

trashheap

an online """""art"""" journal whomst uploads trash
JavaScript
1
star
46

actions-eleventy-test

1
star
47

caginator-test

1
star
48

azure-keyvault-encryption-test

C++
1
star
49

actions-test

C++
1
star
50

presentation_optional_expected

CSS
1
star
51

cpp-con

C++
1
star
52

cpphalloffame

HTML
1
star
53

tartanllama.xyz

HTML
1
star
54

constraint-solver

[OLD] A constraint solver
Common Lisp
1
star