Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::optional<T>::optional

      From cppreference.com
      <cpp‎ |utility‎ |optional
       
       
      Utilities library
       
       
      constexpr optional()noexcept;
      (1)(since C++17)
      constexpr optional(std::nullopt_t)noexcept;
      (2)(since C++17)
      constexpr optional(const optional& other);
      (3)(since C++17)
      constexpr optional( optional&& other)noexcept(/* see below */);
      (4)(since C++17)
      template<class U>
      optional(const optional<U>& other);
      (5)(since C++17)
      (constexpr since C++20)
      (conditionally explicit)
      template<class U>
      optional( optional<U>&& other);
      (6)(since C++17)
      (constexpr since C++20)
      (conditionally explicit)
      template<class...Args>
      constexprexplicit optional(std::in_place_t, Args&&...args);
      (7)(since C++17)
      template<class U,class...Args>

      constexprexplicit optional(std::in_place_t,
                                   std::initializer_list<U> ilist,

                                   Args&&...args);
      (8)(since C++17)
      template<class U=std::remove_cv_t<T>>
      constexpr optional( U&& value);
      (9)(since C++17)
      (conditionally explicit)

      Constructs a newoptional object.

      Contents

      [edit]Parameters

      other - anotheroptional object whose contained value is copied
      value - value with which to initialize the contained value
      args... - arguments with which to initialize the contained value
      ilist - initializer list with which to initialize the contained value

      [edit]Effects

       Overload Initialization methodInitializer for the contained valuehas_value() after construction
      (1)N/A-false
      (2)
      (3)Direct (non-list)*otherother.has_value()
      • Iffalse, the contained value is not initialized.
      (4)std::move(*other)
      (5)*other
      (6)std::move(*other)
      (7)std::forward<Args>(args)...true
      (8)ilist,std::forward<Args>(args)...
      (9)std::forward<U>(value)

      [edit]Constraints and supplement information

      3) Ifstd::is_copy_constructible_v<T> isfalse, the constructor is defined as deleted.
      Ifstd::is_trivially_copy_constructible_v<T> istrue, the constructor is trivial.
      4) This overload participates in overload resolution only ifstd::is_move_constructible_v<T> istrue.
      Ifstd::is_trivially_move_constructible_v<T> istrue, the constructor is trivial.
      5) This overload participates in overload resolution only if all following conditions are satisfied:
      This overload is declared as if withexplicit(!std::is_convertible_v<const U&, T>).
      6) This overload participates in overload resolution only if all following conditions are satisfied:
      This overload is declared as if withexplicit(!std::is_convertible_v<U, T>).
      7) This overload participates in overload resolution only ifstd::is_constructible_v<T, Args...> istrue.
      IfT’s constructor selected for the initialization is aconstexpr constructor, this constructor is also aconstexpr constructor.
      8) This overload participates in overload resolution only ifstd::is_constructible_v<T,std::initializer_list<U>&, Args...> istrue.
      IfT’s constructor selected for the initialization is aconstexpr constructor, this constructor is also aconstexpr constructor.
      9) This overload participates in overload resolution only if all following conditions are satisfied:
      This overload is declared as if withexplicit(!std::is_convertible_v<U, T>).
      IfT’s constructor selected for the initialization is aconstexpr constructor, this constructor is also aconstexpr constructor.
      1. 1.01.1In other words,T is neither constructible nor convertible from any expression of type (possibly const-qualified)std::optional<U>

      [edit]Exceptions

      3) Throws any exception thrown by the constructor ofT.
      4) Throws any exception thrown by the constructor ofT. Has the following
      noexcept specification:  
      5-9) Throws any exception thrown by the constructor ofT.

      [edit]Deduction guides

      [edit]Notes

      Before the resolution ofLWG issue 3836, constructing anstd::optional<bool> fromstd::optional<U> would select overload(9) instead of overloads(5,6) ifU is notbool. This is because overloads(5,6) did not participate in overload resolution ifT (bool in this case) can be constructed or converted fromstd::optional<U>, butstd::optional::operator bool makes the conversion possible for anyU.

      As a result, the constructedstd::optional<bool> always contains a value. That value is determined by whether the providedstd::optional<U> object contains a value, rather than thebool value direct-initialized from the contained value:

      std::optional<bool> op_false(false);std::optional<int> op_zero(0); std::optional<int> from_bool(op_false);// OK: contains 0 (initialized from false)std::optional<bool> from_int(op_zero);// DEFECT (LWG 3836): contains true because// op_zero contains a value, even if initializing// bool from that value gives false
      Feature-test macroValueStdFeature
      __cpp_lib_optional202106L(C++20)
      (DR20)
      Fullyconstexpr(5,6)

      [edit]Example

      Run this code
      #include <iostream>#include <optional>#include <string> int main(){std::optional<int> o1,// empty                       o2=1,// init from rvalue                       o3= o2;// copy-constructor // calls std::string( initializer_list<CharT> ) constructorstd::optional<std::string> o4(std::in_place,{'a','b','c'}); // calls std::string( size_type count, CharT ch ) constructorstd::optional<std::string> o5(std::in_place,3,'A'); // Move-constructed from std::string using deduction guide to pick the type std::optional o6(std::string{"deduction"}); std::cout<<*o2<<' '<<*o3<<' '<<*o4<<' '<<*o5<<' '<<*o6<<'\n';}

      Output:

      1 1 abc AAA deduction

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3836C++17when constructing anstd::optional<bool>
      fromstd::optional<U>, the overload resolution
      would select overload(9) ifU is notbool
      always selects the
      converting copy/move
      constructor in this case
      LWG 3886C++17the default template argument of overload(9) wasTchanged tostd::remove_cv_t<T>
      P0602R4C++17copy/move constructors might not be trivial
      even if underlying constructor is trivial
      required to
      propagate triviality
      P2231R1C++20overloads(5,6) from anotherstd::optional was notconstexprmadeconstexpr

      [edit]See also

      creates anoptional object
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/optional/optional&oldid=180633"

      [8]ページ先頭

      ©2009-2025 Movatter.jp