std::variant<Types...>::operator=

Assigns a new value to an existing variant object.

# Declarations

constexpr variant& operator=( const variant& rhs );

(since C++17)

constexpr variant& operator=( variant&& rhs ) noexcept(/* see below */);

(since C++17)

template< class T >
variant& operator=( T&& t ) noexcept(/* see below */);

(since C++17) (constexpr since C++20)

# Parameters

# Return value

*this

# Notes

Feature-test macro Value Std Feature __cpp_lib_variant 202106L (C++20)(DR) Fully constexpr std::variant (3)

# Example

#include <iomanip>
#include <iostream>
#include <string>
#include <type_traits>
#include <variant>
 
std::ostream& operator<<(std::ostream& os, std::variant<int, std::string> const& va)
{
    os << ": { ";
 
    std::visit([&](auto&& arg)
    {
        using T = std::decay_t<decltype(arg)>;
        if constexpr (std::is_same_v<T, int>)
            os << arg;
        else if constexpr (std::is_same_v<T, std::string>)
            os << std::quoted(arg);
    }, va);
 
    return os << " };\n";
}
 
int main()
{
    std::variant<int, std::string> a{2017}, b{"CppCon"};
    std::cout << "a" << a << "b" << b << '\n';
 
    std::cout << "(1) operator=( const variant& rhs )\n";
    a = b;
    std::cout << "a" << a << "b" << b << '\n';
 
    std::cout << "(2) operator=( variant&& rhs )\n";
    a = std::move(b);
    std::cout << "a" << a << "b" << b << '\n';
 
    std::cout << "(3) operator=( T&& t ), where T is int\n";
    a = 2019;
    std::cout << "a" << a << '\n';
 
    std::cout << "(3) operator=( T&& t ), where T is std::string\n";
    std::string s{"CppNow"};
    std::cout << "s: " << std::quoted(s) << '\n';
    a = std::move(s);
    std::cout << "a" << a << "s: " << std::quoted(s) << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3024C++17copy assignment operator doesn’t participate in overload resolutionif any member type is not copyabledefined as deleted instead
LWG 3585C++17converting assignment was sometimes unexpectedly ill-formedbecause there was no available move assignmentmade well-formed
P0602R4C++17copy/move assignment may not be trivialeven if underlying operations are trivialrequired to propagate triviality
P0608R3C++17converting assignment blindly assembles an overload set,leading to unintended conversionsnarrowing and boolean conversionsnot considered
P2231R1C++20converting assignment (3) was not constexprwhile the required operations can be constexpr in C++20made constexpr

# See also