std::variant<Types...>::emplace
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
std::variant
variant::emplace
(C++26)
template< class T, class... Args >
T& emplace( Args&&... args );
(1)
(since C++17) T& emplace( Args&&... args );
(constexpr since C++20)
template< class T, class U, class... Args >
T& emplace( std::initializer_list <U> il, Args&&... args );
(2)
(since C++17) T& emplace( std::initializer_list <U> il, Args&&... args );
(constexpr since C++20)
template< std::size_t I, class... Args >
std::variant_alternative_t <I, variant>& emplace( Args&&... args );
(3)
(since C++17) std::variant_alternative_t <I, variant>& emplace( Args&&... args );
(constexpr since C++20)
template< std::size_t I, class U, class... Args >
(4)
(since C++17) std::variant_alternative_t <I, variant>&
(constexpr since C++20)
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 participates in overload resolution only if std::is_constructible_v <T, Args...> is true, and
T
occurs exactly once inTypes...
.
2) Equivalent to emplace<I>(il, std::forward <Args>(args)...), where
I
is the zero-based index of T
in Types...
.
- This overload participates in overload resolution only if std::is_constructible_v <T, std::initializer_list <U>&, Args...> is true, and
T
occurs exactly once inTypes...
.
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 participates in overload resolution only if std::is_constructible_v <T_I, Args...> is true.
- It is a compile-time error 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 participates in overload resolution only if std::is_constructible_v <T_I, std::initializer_list <U>&, Args...> is true.
- It is a compile-time error if
I
is not less than sizeof...(Types).
[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 macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_variant |
202106L |
(C++20) (DR) |
Fully constexpr std::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"); // 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
[edit] Defect reports
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 |