Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::optional<T>::emplace

      From cppreference.com
      <cpp‎ |utility‎ |optional
       
       
      Utilities library
       
       
      template<class...Args>
      T& emplace( Args&&...args);
      (1)(since C++17)
      (constexpr since C++20)
      template<class U,class...Args>
      T& emplace(std::initializer_list<U> ilist, Args&&...args);
      (2)(since C++17)
      (constexpr since C++20)

      Constructs the contained value in-place. If*this already contains a value before the call, the contained value is destroyed by calling its destructor.

      1) Initializes the contained value bydirect-initializing (but not direct-list-initializing) withstd::forward<Args>(args)... as parameters.
      2) Initializes the contained value by calling its constructor withilist,std::forward<Args>(args)... as parameters. This overload participates in overload resolution only ifstd::is_constructible<T,std::initializer_list<U>&, Args&&...>::value istrue.

      Contents

      [edit]Parameters

      args... - the arguments to pass to the constructor
      ilist - the initializer list to pass to the constructor
      Type requirements
      -
      T must be constructible fromArgs... for overload(1)
      -
      T must be constructible fromstd::initializer_list andArgs... for overload(2)

      [edit]Return value

      A reference to the new contained value.

      [edit]Exceptions

      Any exception thrown by the selected constructor ofT. If an exception is thrown,*this does not contain a value after this call (the previously contained value, if any, had been destroyed).

      Feature-test macroValueStdFeature
      __cpp_lib_optional202106L(C++20)
      (DR20)
      Fullyconstexpr(1,2)

      [edit]Example

      Run this code
      #include <iostream>#include <optional> struct A{std::string s;     A(std::string str): s(std::move(str)), id{n++}{ note("+ constructed");}    ~A(){ note("~ destructed");}    A(const A& o): s(o.s), id{n++}{ note("+ copy constructed");}    A(A&& o): s(std::move(o.s)), id{n++}{ note("+ move constructed");}     A& operator=(const A& other){        s= other.s;        note("= copy assigned");return*this;}     A& operator=(A&& other){        s= std::move(other.s);        note("= move assigned");return*this;} inlinestaticint n{};int id{};void note(auto s){std::cout<<"  "<< s<<" #"<< id<<'\n';}}; int main(){std::optional<A> opt; std::cout<<"Assign:\n";    opt= A("Lorem ipsum dolor sit amet, consectetur adipiscing elit nec."); std::cout<<"Emplace:\n";// As opt contains a value it will also destroy that value    opt.emplace("Lorem ipsum dolor sit amet, consectetur efficitur."); std::cout<<"End example\n";}

      Output:

      Assign:  + constructed #0  + move constructed #1  ~ destructed #0Emplace:  ~ destructed #1  + constructed #2End example  ~ destructed #2

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2231R1C++20emplace was notconstexpr while the required operations can beconstexpr in C++20madeconstexpr

      [edit]See also

      assigns contents
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/optional/emplace&oldid=172856"

      [8]ページ先頭

      ©2009-2025 Movatter.jp