std::variant::emplace(3)
π std::variant::emplace π
okay bestie, welcome to the C++ Standard Libary manual page for std::variant::emplace β yes, itβs giving βcreate a new value in-place inside an existing variant objectβ and we are gonna get into it. no cap π«Άβ¨
πΈ NAME πΈ
std::variant::emplace β std::variant::emplace
β¨ Synopsis β¨
okay so the signatures are a whole overload buffet. four groups, all numbered, and the constexpr glow-up happens in C++20. slay π
overload (1) β the T-indexed one, no init list
template <class T, class... Args>
T& emplace(Args&&... args);
template <class T, class... Args>
constexpr T& emplace(Args&&... args);
- the
T& emplace(Args&&... args);one is since C++17 and until C++20 - the
constexpr T& emplace(Args&&... args);one is since C++20
overload (2) β the T-indexed one with std::initializer_list<U>
template <class T, class U, class... Args>
T& emplace( std::initializer_list<U> il, Args&&... args );
template <class T, class U, class... Args>
constexpr T& emplace( std::initializer_list<U> il, Args&&... args );
- the non-
constexprone is since C++17 and until C++20 - the
constexprone is since C++20
overload (3) β the I-indexed one, no init list
template <std::size_t I, class... Args>
std::variant_alternative_t<I, variant>& emplace( Args&&... args );
template <std::size_t I, class... Args>
constexpr std::variant_alternative_t<I, variant>& emplace( Args&&... args );
- the non-
constexprone is since C++17 and until C++20 - the
constexprone is since C++20
overload (4) β the I-indexed one with std::initializer_list<U>
template <std::size_t I, class U, class... Args>
std::variant_alternative_t<I, variant>& emplace( std::initializer_list<U> il, Args&&... args );
template <std::size_t I, class U, class... Args>
constexpr std::variant_alternative_t<I, variant>& emplace( std::initializer_list<U> il, Args&&... args );
- the non-
constexprone is since C++17 and until C++20 - the
constexprone is since C++20
Creates a new value in-place, in an existing variant object β thatβs the whole vibe, and hereβs the breakdown, bestie:
- Equivalent to
emplace<I>(std::forward<Args>(args)...), whereIis the zero-based index ofTinTypes....- This overload participates in overload resolution only if
std::is_constructible_v<T, Args...>is true, andToccurs exactly once inTypes....
- This overload participates in overload resolution only if
- Equivalent to
emplace<I>(il, std::forward<Args>(args)...), whereIis the zero-based index ofTinTypes....- This overload participates in overload resolution only if
std::is_constructible_v<T, std::initializer_list<U>&, Args...>is true, andToccurs exactly once inTypes....
- This overload participates in overload resolution only if
- First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type
T_Iwith the argumentsstd::forward<Args>(args).... If an exception is thrown,*thismay becomevalueless_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
Iis not less thansizeof...(Types).
- This overload participates in overload resolution only if
- First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type
T_Iwith the argumentsil,std::forward<Args>(args).... If an exception is thrown,*thismay becomevalueless_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
Iis not less thansizeof...(Types).
- This overload participates in overload resolution only if
π Parameters π
argsβ constructor arguments to use when constructing the new valueilβinitializer_listargument to use when constructing the new value
π¦ Return value π¦
A reference to the new contained value. periodt β¨
π Exceptions π
1-4) Any exception thrown during the initialization of the contained value.
π Example π
the example literally says // Run this code, so bestie, run it and watch the magic:
#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
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
P2231R1, applied to C++20- Behavior as published:
emplacewas notconstexprwhile the required operations can beconstexprin C++20 - Correct behavior: made
constexpr
- Behavior as published:
π See also π
operator=β assigns a variant (public member function)
signed, sealed, slayed: source cppreference.com, version/date 2024.06.10, and the page is std::variant::emplace(3) π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms