| (1) | ||
template <class T, class... Args> T& emplace(Args&&... args); | (since C++17) (until C++20) | |
template <class T, class... Args> constexpr T& emplace(Args&&... args); | (since C++20) | |
| (2) | ||
template <class T, class U, class... Args> T& emplace( std::initializer_list<U> il, Args&&... args ); | (since C++17) (until C++20) | |
template <class T, class U, class... Args> constexpr T& emplace( std::initializer_list<U> il, Args&&... args ); | (since C++20) | |
| (3) | ||
template <std::size_t I, class... Args> std::variant_alternative_t<I, variant>& emplace( Args&&... args ); | (since C++17) (until C++20) | |
template <std::size_t I, class... Args> constexpr std::variant_alternative_t<I, variant>& emplace( Args&&... args ); | (since C++20) | |
| (4) | ||
template <std::size_t I, class U, class... Args>
std::variant_alternative_t<I, variant>&
emplace( std::initializer_list<U> il, Args&&... args );
| (since C++17) (until C++20) | |
template <std::size_t I, class U, class... Args>
constexpr std::variant_alternative_t<I, variant>&
emplace( std::initializer_list<U> il, Args&&... args );
| (since C++20) |
Creates a new value in-place, in an existing variant object.
emplace<I>(std::forward<Args>(args)...), where I is the zero-based index of T in Types.... std::is_constructible_v<T, Args...> is true, and T occurs exactly once in Types...
emplace<I>(il, std::forward<Args>(args)...), where I is the zero-based index of T in Types.... std::is_constructible_v<T, std::initializer_list<U>&, Args...> is true, and T occurs exactly once in Types...
T_I with the arguments std::forward<Args>(args).... If an exception is thrown, *this may become valueless_by_exception. std::is_constructible_v<T_I, Args...> is true. I is not less than sizeof...(Types).T_I with the arguments il, std::forward<Args>(args).... If an exception is thrown, *this may become valueless_by_exception. std::is_constructible_v<T_I, std::initializer_list<U>&, Args...> is true. I is not less than sizeof...(Types).| args | - | constructor arguments to use when constructing the new value |
| il | - | initializer_list argument to use when constructing the new value |
A reference to the new contained value.
#include <iostream>
#include <string>
#include <variant>
int main()
{
std::variant<std::string> v1;
v1.emplace<0>("abc"); // OK
std::cout << std::get<0>(v1) << '\n';
v1.emplace<std::string>("def"); // OK
std::cout << std::get<0>(v1) << '\n';
std::variant<std::string, std::string> v2;
v2.emplace<1>("ghi"); // OK
std::cout << std::get<1>(v2) << '\n';
// v2.emplace<std::string>("abc"); -> Error
}Output:
abc def ghi
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P2231R1 | C++20 | emplace was not constexpr while the required operations can be constexpr in C++20 | made constexpr |
| assigns a variant (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/variant/emplace