Namespaces
Variants
Views
Actions

std::expected

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
expected
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
std::expected
Member functions
Observers
Modifiers
Non-member functions
(C++23)
(C++23)
Helper classes
(C++23)
(C++23)(C++23)
 
Defined in header <expected>
template< class T, class E >
class expected;
(since C++23)

The class template std::expected provides a way to store either of two values. An object of std::expected at any given time either holds an expected value of type T, or an unexpected value of type E. std::expected is never valueless.

The stored value is allocated directly within the storage occupied by the expected object. No dynamic memory allocation takes place.

A program is ill-formed if it instantiates an expected with a reference type, a function type, or a specialization of std::unexpected. In addition, T must not be std::in_place_t or std::unexpect_t.

Contents

[edit] Template parameters

T - the type of the expected value. The type must either be (possibly cv-qualified) void, or meet the Destructible requirements (in particular, array and reference types are not allowed).
E - the type of the unexpected value. The type must meet the Destructible requirements, and must be a valid template argument for std::unexpected (in particular, arrays, non-object types, and cv-qualified types are not allowed).

[edit] Member types

Member type Definition
value_type (C++23) T
error_type (C++23) E
unexpected_type (C++23) std::unexpected<E>
rebind (C++23) template< class U >

using rebind = expected<U, error_type>;

[edit] Member functions

constructs the expected object
(public member function) [edit]
destroys the expected object, along with its contained value
(public member function) [edit]
(C++23)
assigns contents
(public member function) [edit]
Observers
accesses the expected value
(public member function) [edit]
checks whether the object contains an expected value
(public member function) [edit]
(C++23)
returns the expected value
(public member function) [edit]
(C++23)
returns the unexpected value
(public member function) [edit]
(C++23)
returns the expected value if present, another value otherwise
(public member function) [edit]
Monadic operations
(C++23)
returns the result of the given function on the expected value if it exists; otherwise, returns the expected itself
(public member function) [edit]
(C++23)
returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself
(public member function) [edit]
(C++23)
returns the expected itself if it contains an expected value; otherwise, returns the result of the given function on the unexpected value
(public member function) [edit]
returns the expected itself if it contains an expected value; otherwise, returns an expected containing the transformed unexpected value
(public member function) [edit]
Modifiers
(C++23)
constructs the expected value in-place
(public member function) [edit]
(C++23)
exchanges the contents
(public member function) [edit]

[edit] Non-member functions

compares expected objects
(function template) [edit]
specializes the std::swap algorithm
(function) [edit]

[edit] Helper classes

represented as an unexpected value
(class template) [edit]
exception indicating checked access to an expected that contains an unexpected value
(class template) [edit]
in-place construction tag for unexpected value in expected
(class) (constant) [edit]

[edit] Notes

Types with the same functionality are called Result in Rust and Either in Haskell.

Feature-test macro Value Std Comment
__cpp_lib_expected 202202L (C++23) class template std::expected and associated helper classes
202211L (C++23) Monadic functions for std::expected

[edit] Example

#include <expected>
#include <iostream>
#include <string_view>
 
enum class parse_error {
    invalid_char,
    overflow
};
 
std::expected<double, parse_error>
parse_number(std::string_view& str) {
    const char* begin = str.data();
    char* end;
    double retval = std::strtod(begin, &end);
 
    if (begin == end) {
        return std::unexpected(parse_error::invalid_char);
    }
    // else if (retval == std::numeric_limits<double>::infinity) {
    //     return std::unexpected(parse_error::overflow);
    // }
 
    str.remove_prefix(end - begin);
    return retval;
}
 
auto main(void) -> int {
 
    std::string_view src = "12";
    // change the src to "meow" or something invalid
    // that is can not be converted to a number and observe the output
    auto num = parse_number(src);
 
    if (num.has_value()) {
        // if num does not have a value
        // dereferencing num will cause an undefined behaviour
        std::cout << *num << "\n";
        // num.value():     throws std::bad_expected_access
        // num.value_or(0): uses specified default value
    }
    else if (num.error() == parse_error::invalid_char) {
        // handle ...
    }
    else if (num.error() == parse_error::overflow) {
        // handle ...
    }
    else {
        // do another thing
    }
 
    return EXIT_SUCCESS;
}

Output:

12

[edit] See also

(C++17)
a type-safe discriminated union
(class template) [edit]
(C++17)
a wrapper that may or may not hold an object
(class template) [edit]