Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unexpected

      From cppreference.com
      <cpp‎ |utility‎ |expected
       
       
      Utilities library
       
       
      Defined in header<expected>
      template<class E>
      class unexpected;
      (since C++23)

      The class templatestd::unexpected represents an unexpected value stored instd::expected. In particular,std::expected has constructors withstd::unexpected as a single argument, which creates anexpected object that contains an unexpected value.

      A program is ill-formed if it instantiates anunexpected with a non-object type, an array type, a specialization ofstd::unexpected, or a cv-qualified type.

      Contents

      [edit]Template parameters

      E - the type of the unexpected value. The type must not be an array type, a non-object type, a specialization ofstd::unexpected, or a cv-qualified type.

      [edit]Member functions

      constructs theunexpected object
      (public member function)
      (destructor)
      (implicitly declared)
      destroys theunexpected object, along with the stored value
      (public member function)
      operator=
      (implicitly declared)
      assigns the stored value
      (public member function)
      accesses the stored value
      (public member function)
      swaps the stored value
      (public member function)

      [edit]Non-member functions

      (C++23)
      compares the stored value
      (function template)
      specializes thestd::swap algorithm
      (function template)

      std::unexpected::unexpected

      constexpr unexpected(const unexpected&)=default;
      (1)
      constexpr unexpected( unexpected&&)=default;
      (2)
      template<class Err= E>
      constexprexplicit unexpected( Err&& e);
      (3)
      template<class...Args>
      constexprexplicit unexpected(std::in_place_t, Args&&...args);
      (4)
      template<class U,class...Args>

      constexprexplicit unexpected(std::in_place_t,

                                     std::initializer_list<U> il, Args&&...args);
      (5)

      Constructs astd::unexpected object.

      1,2) Copy/move constructor. Copies or moves the stored value, respectively.
      3) Constructs the stored value, as if bydirect-initializing a value of typeE fromstd::forward<Err>(e).
      4) Constructs the stored value, as if bydirect-initializing a value of typeE from the argumentsstd::forward<Args>(args)....
      5) Constructs the stored value, as if bydirect-initializing a value of typeE from the argumentsil,std::forward<Args>(args)....

      Parameters

      e - 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

      Exceptions

      Throws any exception thrown by the constructor ofE.

      std::unexpected::error

      constexprconst E& error()const&noexcept;

      constexpr E& error()&noexcept;
      constexprconst E&& error()const&&noexcept;

      constexpr E&& error()&&noexcept;

      Returns a reference to the stored value.

      std::unexpected::swap

      constexprvoid swap( unexpected& other)noexcept(std::is_nothrow_swappable_v<E>);

      Swaps the stored values, as if byusingstd::swap; swap(error(), other.error());.

      The program is ill-formed ifstd::is_swappable_v<E> is false.

      operator==(std::unexpected)

      template<class E2>
      friendconstexprbool operator==( unexpected& x, std::unexpected<E2>& y);

      Compares the stored values, as if byreturn x.error()== y.error().

      If the expressionx.error()== e.error() is not well-formed, or if its result is not convertible tobool, the program is ill-formed.

      This function is not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup whenstd::unexpected<E> is an associated class of the arguments.

      swap(std::unexpected)

      friendconstexprvoid
      swap( unexpected& x, unexpected& y)noexcept(noexcept(x.swap(y)));

      Equivalent tox.swap(y).

      This overload participates in overload resolution only ifstd::is_swappable_v<E> is true.

      This function is not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup whenstd::unexpected<E> is an associated class of the arguments.

      [edit]Deduction guides

      template<class E>
      unexpected(E)-> unexpected<E>;
      (since C++23)

      Thededuction guide is provided forunexpected to allow deduction from the constructor argument.

      [edit]Notes

      Prior to C++17, the namestd::unexpected denoted the function called by the C++ runtime when a dynamic exception specification was violated.

      [edit]Example

      Run this code
      #include <expected>#include <iostream> enumclass error{    compile_time_error,    runtime_error}; [[nodiscard]]auto unexpected_runtime_error()->std::expected<int, error>{return std::unexpected(error::runtime_error);} int main(){std::expected<double,int> ex= std::unexpected(3); if(!ex)std::cout<<"ex contains an error value\n"; if(ex== std::unexpected(3))std::cout<<"The error value is equal to 3\n"; constauto e= unexpected_runtime_error();     e.and_then([](constauto& e)->std::expected<int, error>{std::cout<<"and_then: "<<int(e);// not printedreturn{};})    .or_else([](constauto& e)->std::expected<int, error>{std::cout<<"or_else: "<<int(e);// prints this linereturn{};});}

      Output:

      ex contains an error valueThe error value is equal to 3or_else: 1

      [edit]See also

      constructs theexpected object
      (public member function)[edit]
      returns the expected value
      (public member function)[edit]
      exchanges the contents
      (public member function)[edit]
      specializes thestd::swap algorithm
      (function)[edit]
      (C++23)
      comparesexpected objects
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/expected/unexpected&oldid=183240"

      [8]ページ先頭

      ©2009-2025 Movatter.jp