Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant<Types...>::emplace

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

      std::variant_alternative_t<I, variant>&

          emplace(std::initializer_list<U> il, Args&&...args);
      (4)(since C++17)
      (constexpr since C++20)

      Creates a new value in-place, in an existingvariant object

      1) Equivalent toemplace<I>(std::forward<Args>(args)...), whereI is the zero-based index ofT inTypes....
      • This overload participates in overload resolution only ifstd::is_constructible_v<T, Args...> istrue, andT occurs exactly once inTypes....
      2) Equivalent toemplace<I>(il,std::forward<Args>(args)...), whereI is the zero-based index ofT inTypes....
      3) First, destroys the currently contained value (if any). Thendirect-initializes the contained value as if constructing a value of typeT_I with the argumentsstd::forward<Args>(args).... If an exception is thrown,*this may becomevalueless_by_exception.
      • This overload participates in overload resolution only ifstd::is_constructible_v<T_I, Args...> istrue.
      • It is a compile-time error ifI is not less thansizeof...(Types).
      4) First, destroys the currently contained value (if any). Thendirect-initializes the contained value as if constructing a value of typeT_I with the argumentsil,std::forward<Args>(args).... If an exception is thrown,*this may becomevalueless_by_exception.

      Contents

      [edit]Parameters

      args - constructor arguments to use when constructing the new value
      il - initializer_list argument to use when constructing the new value

      [edit]Return value

      A reference to the new contained value.

      [edit]Exceptions

      1-4) Any exception thrown during the initialization of the contained value.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_variant202106L(C++20)
      (DR)
      Fullyconstexprstd::variant(1-4)

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <variant> int main(){std::variant<std::string> v1;    v1.emplace<0>("abc");// OKstd::cout<< std::get<0>(v1)<<'\n';    v1.emplace<std::string>("def");// OKstd::cout<< std::get<0>(v1)<<'\n'; std::variant<std::string,std::string> v2;    v2.emplace<1>("ghi");// OKstd::cout<< std::get<1>(v2)<<'\n';// v2.emplace<std::string>("abc"); -> Error}

      Output:

      abcdefghi

      [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 avariant
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/emplace&oldid=172839"

      [8]ページ先頭

      ©2009-2025 Movatter.jp