std::variant<Types...>::emplace (3) - Linux Manuals
std::variant<Types...>::emplace: std::variant<Types...>::emplace
NAME
std::variant<Types...>::emplace - std::variant<Types...>::emplace
Synopsis
template <class T, class... Args> (1) (since C++17)
T& emplace(Args&&... args);
template <class T, class U, class... Args> (2) (since C++17)
T& emplace(std::initializer_list<U> il, Args&&... args);
template <size_t I, class... Args> (3) (since C++17)
std::variant_alternative_t<I, variant>& emplace(Args&&... args);
template <size_t I, class U, class... Args> (4) (since C++17)
std::variant_alternative_t<I, variant>& emplace(std::initializer_list<U> il, Args&&... args);
Creates a new value in-place, in an existing variant object
1) Equivalent to emplace<I>(std::forward<Args>(args)...), where I is the zero-based index of T in Types.... This overload only participates in overload resolution if std::is_constructible_v<T, Args...> is true, and T occurs exactly once in Types...
2) Equivalent to emplace<I>(il, std::forward<Args>(args)...), where I is the zero-based index of T in Types.... This overload only participates in overload resolution if std::is_constructible_v<T, std::initializer_list<U>&, Args...> is true, and T occurs exactly once in Types...
3) First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type T_I with the arguments std::forward<Args>(args).... If an exception is thrown, *this may become valueless_by_exception. This overload only participates in overload resolution if std::is_constructible_v<T_I, Args...> is true. The behavior is undefined if I is not less than sizeof...(Types).
4) First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type T_I with the arguments il, std::forward<Args>(args).... If an exception is thrown, *this may become valueless_by_exception. This overload only participates in overload resolution if std::is_constructible_v<T_I, initializer_list<U>&, Args...> is true. The behavior is undefined if I is not less than sizeof...(Types).
Parameters
args - constructor arguments to use when constructing the new value
il - initializer_list argument to use when constructing the new value
Return value
A reference to the new contained value.
Exceptions
1-4) Any exception thrown during the initialization of the contained value.
Example
// Run this code
Output:
See also
operator= (public member function)