Namespaces
Variants
Views
Actions

std::expected<T,E>::expected

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
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)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
constexpr expected() noexcept(/* see below */);
(1) (since C++23)
constexpr expected( const expected& other );
(2) (since C++23)
constexpr expected( expected&& other ) noexcept(/* see below */);
(3) (since C++23)
template< class U, class G >
constexpr explicit(/* see below */) expected( const expected<U, G>& other );
(4) (since C++23)
template< class U, class G >
constexpr explicit(/* see below */) expected( expected<U, G>&& other );
(5) (since C++23)
template< class U = T >
constexpr explicit(!std::is_convertible_v<U, T>) expected( U&& v );
(6) (since C++23)
(T is not cv void)
template< class G >

constexpr explicit(!std::is_convertible_v<const G&, E>)

    expected( const std::unexpected<G>& e );
(7) (since C++23)
template< class G >

constexpr explicit(!std::is_convertible_v<G, E>)

    expected( std::unexpected<G>&& e );
(8) (since C++23)
template< class... Args >
constexpr explicit expected( std::in_place_t, Args&&... args );
(9) (since C++23)
(T is not cv void)
template< class U, class... Args >

constexpr explicit expected( std::in_place_t,

                             std::initializer_list<U> il, Args&&... args );
(10) (since C++23)
(T is not cv void)
template< class... Args >
constexpr explicit expected( std::in_place_t ) noexcept;
(11) (since C++23)
(T is cv void)
template< class... Args >
constexpr explicit expected( std::unexpect_t, Args&&... args );
(12) (since C++23)
template< class U, class... Args >

constexpr explicit expected( std::unexpect_t,

                             std::initializer_list<U> il, Args&&... args );
(13) (since C++23)

Constructs a new expected object.

1) Default constructor. If T is not (possibly cv-qualified) void, constructs an object that contains an expected value, which is value-initialized.

After construction, has_value() returns true.

2) Copy constructor. If other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from other.error(). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from *other.

After construction, has_value() is equal to other.has_value().

3) Move constructor. If other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from std::move(other.error()). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from std::move(*other).

After construction, has_value() is equal to other.has_value().

4,5) Let

If other.has_value() is false, the new object contains an unexpected value, which is direct-initialized from std::forward<GF>(other.error()). Otherwise, if T is not (possibly cv-qualified) void, the new object contains an expected value, which is direct-initialized from std::forward<UF>(*other). After construction, has_value() is equal to other.has_value().

6) Constructs an object that contains an expected value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::forward<U>(v).

After construction, has_value() returns true.

7,8) Let GF be const G& for (7) and G for (8).

Constructs an object that contains an unexpected value, which is direct-initialized from std::forward<GF>(e.error()). After construction, has_value() returns false.

9) Constructs an object that contains an expected value, which is direct-initialized from the arguments std::forward<Args>(args)....

After construction, has_value() returns true.

10) Constructs an object that contains an expected value, which is direct-initialized from the arguments il, std::forward<Args>(args)....

After construction, has_value() returns true.

11) Constructs an object such that after construction, has_value() returns true.
12) Constructs an object that contains an unexpected value, which is direct-initialized from the arguments std::forward<Args>(args)....

After construction, has_value() returns false.

13) Constructs an object that contains an unexpected value, which is direct-initialized from the arguments il, std::forward<Args>(args)....

After construction, has_value() returns false.

Contents

[edit] Parameters

other - another expected object whose contained value is copied
e - std::unexpected object whose contained value is copied
v - value with which to initialize the contained value
args... - arguments with which to initialize the contained value
il - initializer list with which to initialize the contained value

[edit] Exceptions

1) Throws any exception thrown by the constructor of T. If T is (possibly cv-qualified) void,
noexcept specification:  
noexcept
  
2) Throws any exception thrown by the constructor of T or E.
3) If T is (possibly cv-qualified) void,
noexcept specification:  
Otherwise,
4,5) Throws any exception thrown by the constructor of T or E.
6) Throws any exception thrown by the constructor of T.
7,8) Throws any exception thrown by the constructor of E.
9,10) Throws any exception thrown by the constructor of T.
12,13) Throws any exception thrown by the constructor of E.

[edit] Example

[edit] See also

represented as an unexpected value
(class template) [edit]
in-place construction tag
(tag) [edit]
in-place construction tag for unexpected value in expected
(class) (constant) [edit]