Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::optional<T>::optional

      From cppreference.com
      <cpp‎ |experimental‎ |optional
       
       
       
       
       
      constexpr optional()noexcept;
      constexpr optional(std::experimental::nullopt_t)noexcept;
      (1)(library fundamentals TS)
      optional(const optional& other);
      (2)(library fundamentals TS)
      optional( optional&& other)noexcept(/* see below */);
      (3)(library fundamentals TS)
      constexpr optional(const T& value);
      (4)(library fundamentals TS)
      constexpr optional( T&& value);
      (5)(library fundamentals TS)
      template<class...Args>
      constexprexplicit optional(std::experimental::in_place_t, Args&&...args);
      (6)(library fundamentals TS)
      template<class U,class...Args>

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

                                   Args&&...args);
      (7)(library fundamentals TS)

      Constructs a newoptional object.

      1) Constructs the object thatdoes not contain a value.
      2) Copy constructor: Ifother contains a value, initializes the contained value as ifdirect-initializing (but not direct-list-initializing) an object of typeT with the expression*other. Ifother does not contain a value, constructs an object thatdoes not contain a value.
      3) Move constructor: Ifother contains a value, initializes the contained value as ifdirect-initializing (but not direct-list-initializing) an object of typeT with the expressionstd::move(*other) anddoes not makeother empty: a moved-from optional stillcontains a value, but the value itself is moved from. Ifother does not contain a value, constructs an object thatdoes not contain a value.
      4) Constructs an optional object thatcontains a value, initialized as ifdirect-initializing (but not direct-list-initializing) an object of typeT with the expressionvalue. This constructor isconstexpr if the constructor ofT selected by direct-initialization isconstexpr.
      5) Constructs an optional object thatcontains a value, initialized as ifdirect-initializing (but not direct-list-initializing) an object of typeT with the expressionstd::move(value). This constructor isconstexpr if the constructor ofT selected by direct-initialization isconstexpr.
      6) Constructs an optional object thatcontains a value, initialized as ifdirect-initializing (but not direct-list-initializing) an object of typeT from the argumentsstd::forward<Args>(args)....
      7) Constructs an optional object thatcontains a value, initialized as ifdirect-initializing (but not direct-list-initializing) an object of typeT from the argumentsilist,std::forward<Args>(args).... The function does not participate in the overload resolution ifstd::is_constructible<T,std::initializer_list<U>&, Args&&...>::value!=true.

      Contents

      [edit]Parameters

      other - anotheroptional object whose contained value to copy
      value - value to initialize the contained value with
      args... - arguments to initialize the contained value with
      ilist - initializer list to initialize the contained value with
      Type requirements
      -
      T must meet the requirements ofCopyConstructible in order to use overloads (2,4).
      -
      T must meet the requirements ofMoveConstructible in order to use overloads (3,5).

      [edit]Exceptions

      2) Throws any exception thrown by the constructor ofT.
      3) Throws any exception thrown by the constructor ofT. Has the followingnoexcept declaration:
      noexcept specification:  
      4-7) Throws any exception thrown by the constructor ofT.

      [edit]Example

      Run this code
      #include <experimental/optional>#include <iostream>#include <string> int main(){std::experimental::optional<int> o1,// empty                                     o2=1,// init from rvalue                                     o3= o2;// copy-constructor std::experimental::optional<std::string> o4(std::experimental::in_place,{'a','b','c'}); std::cout<<*o2<<' '<<*o3<<' '<<*o4<<'\n';}

      Output:

      1 1 abc

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp