std::bad_expected_access - cppreference.com (original) (raw)

Defined in header
template< class E > class bad_expected_access : public std::bad_expected_access<void> (1) (since C++23)
template<> class bad_expected_access<void> : public std::exception (2) (since C++23)
  1. Defines a type of object to be thrown by

std::expected::value

when accessing an expected object that contains an unexpected value. bad_expected_access<E> stores a copy of the unexpected value.

  1. bad_expected_access<void> is the base class of all other bad_expected_access specializations.
All member functions of std::bad_expected_access are constexpr: it is possible to create and use std::bad_expected_access objects in the evaluation of a constant expression.However, std::bad_expected_access objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression. (since C++26)

Contents

[edit] Members of the primary template

| | constructs a bad_expected_access object (public member function) | | --------------------------------------------------------------------- | | | returns the stored value (public member function) | | | returns the explanatory string (public member function) |

std::bad_expected_access::bad_expected_access

| explicit bad_expected_access( E e ); | | (since C++23) (constexpr since C++26) | | -------------------------------------- | | ------------------------------------- |

Constructs a new bad_expected_access<E> object. Initializes the stored value with std::move(e).

std::bad_expected_access::error

const E& error() const & noexcept; (1) (since C++23) (constexpr since C++26)
E& error() & noexcept; (2) (since C++23) (constexpr since C++26)
const E&& error() const && noexcept; (3) (since C++23) (constexpr since C++26)
E&& error() && noexcept; (4) (since C++23) (constexpr since C++26)

Returns a reference to the stored value.

std::bad_expected_access::what

| const char* what() const noexcept override; | | (since C++23) (constexpr since C++26) | | -------------------------------------------- | | ------------------------------------- |

Returns the explanatory string.

Return value

Pointer to a null-terminated string with explanatory information. The string is suitable for conversion and display as a std::wstring. The pointer is guaranteed to be valid at least until the exception object from which it is obtained is destroyed, or until a non-const member function (e.g., copy assignment operator) on the exception object is called.

The returned string is encoded with the ordinary literal encoding during constant evaluation. (since C++26)

Notes

Implementations are allowed but not required to override what().

Inherited from std::bad_expected_access<void>

Members of the bad_expected_access<void> specialization

| | constructs a bad_expected_access<void> object (protected member function) | | ------------------------------------------------------------------------------- | | | destroys the bad_expected_access<void> object (protected member function) | | | replaces the bad_expected_access<void> object (protected member function) | | | returns the explanatory string (public member function) |

Special member functions of bad_expected_access<void> are protected. They can only be called by derived classes.

Inherited from std::exception

Member functions

| | destroys the exception object (virtual public member function of std::exception) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | returns an explanatory string (virtual public member function of std::exception) [edit] |

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_constexpr_exceptions 202502L (C++26) constexpr std::bad_expected_access

[edit] Example

[edit] See also