Scroll to navigation

std::variant::variant(3) C++ Standard Libary std::variant::variant(3)

NAME

std::variant::variant - std::variant::variant

Synopsis


constexpr variant() noexcept(/* see below */); (1) (since C++17)
constexpr variant( const variant& other ); (2) (since C++17)
constexpr variant( variant&& other ) noexcept(/* see below */); (3) (since C++17)
template< class T > (4) (since C++17)
constexpr variant( T&& t ) noexcept(/* see below */);
template< class T,


class... Args > (5) (since C++17)
constexpr explicit variant( std::in_place_type_t<T>,


Args&&... args );
template< class T,


class U,
class... Args > (6) (since C++17)
constexpr explicit variant( std::in_place_type_t<T>,
std::initializer_list<U> il,


Args&&... args );
template< std::size_t I,


class... Args > (7) (since C++17)
constexpr explicit variant( std::in_place_index_t<I>,


Args&&... args );
template< std::size_t I,


class U,
class... Args > (8) (since C++17)
constexpr explicit variant( std::in_place_index_t<I>,
std::initializer_list<U> il,


Args&&... args );


Constructs a new variant object.


1) Default constructor. Constructs a variant holding the value-initialized value of
the first alternative (index() is zero).


* This constructor is constexpr if and only if the value initialization of the
alternative type T_0 would satisfy the requirements for a constexpr function.
* This overload participates in overload resolution only if
std::is_default_constructible_v<T_0> is true.


2) Copy constructor. If other is not valueless_by_exception, constructs a variant
holding the same alternative as other and direct-initializes the contained value
with std::get<other.index()>(other). Otherwise, initializes a valueless_by_exception
variant.


* This constructor is defined as deleted unless std::is_copy_constructible_v<T_i>
is true for all T_i in Types....
* It is trivial if std::is_trivially_copy_constructible_v<T_i> is true for all T_i
in Types....


3) Move constructor. If other is not valueless_by_exception, constructs a variant
holding the same alternative as other and direct-initializes the contained value
with std::get<other.index()>(std::move(other)). Otherwise, initializes a
valueless_by_exception variant.


* This overload participates in overload resolution only if
std::is_move_constructible_v<T_i> is true for all T_i in Types....
* It is trivial if std::is_trivially_move_constructible_v<T_i> is true for all T_i
in Types....


4) Converting constructor. Constructs a variant holding the alternative type T_j
that would be selected by overload resolution for the expression
F(std::forward<T>(t)) if there was an overload of imaginary function F(T_i) for
every T_i from Types... in scope at the same time, except that:


* An overload F(T_i) is only considered if the declaration T_i x[] = {
std::forward<T>(t) }; is valid for some invented variable x;


Direct-initializes the contained value as if by direct non-list-initialization from
std::forward<T>(t).


* This overload participates in overload resolution only if


* sizeof...(Types) > 0,
* std::decay_t<U>
(until C++20)
std::remove_cvref_t<U>
(since C++20) is neither the same type as variant, nor a specialization of
std::in_place_type_t, nor a specialization of std::in_place_index_t,
* std::is_constructible_v<T_j, T> is true,
* and the expression F(std::forward<T>(t)) (with F being the above-mentioned
set of imaginary functions) is well formed.


* This constructor is a constexpr constructor if T_j's selected constructor is a
constexpr constructor.


std::variant<std::string> v("abc"); // OK
std::variant<std::string, std::string> w("abc"); // ill-formed
std::variant<std::string, const char*> x("abc"); // OK, chooses const char*
std::variant<std::string, bool> y("abc"); // OK, chooses string; bool is not a candidate
std::variant<float, long, double> z = 0; // OK, holds long
// float and double are not candidates


5) Constructs a variant with the specified alternative T and initializes the
contained value with the arguments std::forward<Args>(args)....


* If T's selected constructor is a constexpr constructor, this constructor is also
a constexpr constructor.
* This overload participates in overload resolution only if there is exactly one
occurrence of T in Types... and std::is_constructible_v<T, Args...> is true.


6) Constructs a variant with the specified alternative T and initializes the
contained value with the arguments il, std::forward<Args>(args)....


* If T's selected constructor is a constexpr constructor, this constructor is also
a constexpr constructor.
* This overload participates in overload resolution only if there is exactly one
occurrence of T in Types... and std::is_constructible_v<T, initializer_list<U>&,
Args...> is true.


7) Constructs a variant with the alternative T_i specified by the index I and
initializes the contained value with the arguments std::forward<Args>(args)....


* If T_i's selected constructor is a constexpr constructor, this constructor is
also a constexpr constructor.
* This overload participates in overload resolution only if I < sizeof...(Types)
and std::is_constructible_v<T_i, Args...> is true.


8) Constructs a variant with the alternative T_i specified by the index I and
initializes the contained value with the arguments il, std::forward<Args>(args)....


* If T_i's selected constructor is a constexpr constructor, this constructor is
also a constexpr constructor.
* This overload participates in overload resolution only if I < sizeof...(Types)
and std::is_constructible_v<T_i, std::initializer_list<U>&, Args...> is true.

Parameters


other - another variant object whose contained value to copy/move
t - value to initialize the contained value with
args... - arguments to initialize the contained value with
il - initializer list to initialize the contained value with

Exceptions


1) May throw any exception thrown by the value initialization of the first
alternative.
noexcept specification:
noexcept(std::is_nothrow_default_constructible_v<T_0>)
2) May throw any exception thrown by direct-initializing any T_i in Types...
3) May throw any exception thrown by move-constructing any T_i in Types....
noexcept specification:
noexcept( (std::is_nothrow_move_constructible_v<Types> && ...))
4) May throw any exception thrown by the initialization of the selected alternative
T_j.
noexcept specification:
noexcept(std::is_nothrow_constructible_v<T_j, T>)
5-8) May throw any exception thrown by calling the selected constructor of the
selected alternative

Example

// Run this code


#include <cassert>
#include <iostream>
#include <string>
#include <variant>
#include <vector>


template <class Os> Os& operator<< (Os& os, const std::vector<int>& v) {
os << "{ ";
for (int e: v) { std::cout << e << ' '; }
return os << "}";
}


int main()
{
{
std::variant<int, std::string> var; // value-initializes first alternative
assert(std::holds_alternative<int>(var) && var.index() == 0 &&
std::get<int>(var) == 0);
}
{
std::variant<std::string, int> var{"STR"};
// initializes first alternative with std::string{"STR"};
assert(var.index() == 0);
std::cout << "1) " << std::get<std::string>(var) << '\n';
}
{
std::variant<std::string, int> var{42};
// initializes second alternative with int = 42;
assert(std::holds_alternative<int>(var));
std::cout << "2) " << std::get<int>(var) << '\n';
}
{
std::variant<std::string, std::vector<int>, float> var{
std::in_place_type<std::string>, 4, 'A'};
// initializes first alternative with std::string{4, 'A'};
assert(var.index() == 0);
std::cout << "3) " << std::get<std::string>(var) << '\n';
}
{
std::variant<std::string, std::vector<int>, char> var{
std::in_place_type<std::vector<int>>, {1,2,3,4,5} };
// initializes second alternative with std::vector{1,2,3,4,5};
assert(var.index() == 1);
std::cout << "4) " << std::get<std::vector<int>>(var) << '\n';
}
{
std::variant<std::string, std::vector<int>, bool> var{
std::in_place_index<0>, "ABCDE", 3};
// initializes first alternative with std::string{"ABCDE", 3};
assert(var.index() == 0);
std::cout << "5) " << std::get<std::string>(var) << '\n';
}
{
std::variant<std::string, std::vector<int>, char> var{
std::in_place_index<1>, 4, 42};
// initializes second alternative with std::vector(4, 42);
assert(std::holds_alternative<std::vector<int>>(var));
std::cout << "6) " << std::get<std::vector<int>>(var) << '\n';
}
}

Output:


1) STR
2) 42
3) AAAA
4) { 1 2 3 4 5 }
5) ABC
6) { 42 42 42 42 }


Defect reports


The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.


DR Applied to Behavior as published Correct behavior
allocator-aware constructors
LWG 2901 C++17 provided but constructors removed
variant can't properly support
allocators
converting constructor template
P0739R0 C++17 interacts constraint added
poorly with class template argument
deduction
copy constructor doesn't participate
LWG 3024 C++17 in defined as deleted instead
overload resolution if any member
type is not copyable
copy/move constructors may not be required to propagate
P0602R4 C++17 trivial even if underlying triviality
constructors are trivial
converting constructor blindly
P0608R3 C++17 assembles narrowing and boolean
an overload set, leading to conversions not considered
unintended conversions
Pointer to bool conversion
converting constructor for bool did is narrowing and
P1957R2 C++17 not allow converting constructor has
implicit conversion no
exception for bool

2022.07.31 http://cppreference.com