|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Modifiers | ||||
| Visitation | ||||
(C++26) | ||||
| Non-member functions | ||||
swap(std::variant) | ||||
| Helper classes | ||||
| Helper objects | ||||
Defined in header <variant> | ||
template<class...Types> void swap(std::variant<Types...>& lhs, | (since C++17) (constexpr since C++20) | |
Overloads thestd::swap algorithm forstd::variant. Effectively callslhs.swap(rhs).
This overload participates in overload resolution only ifstd::is_move_constructible_v<T_i> andstd::is_swappable_v<T_i> are bothtrue for allT_i inTypes....
Contents |
| lhs, rhs | - | variant objects whose values to swap |
(none)
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_variant | 202106L | (C++20) (DR) | Fullyconstexprstd::variant |
#include <iostream>#include <string>#include <variant> void print(autoconst& v,char term='\n'){std::visit([](auto&& o){std::cout<< o;}, v);std::cout<< term;} int main(){std::variant<int,std::string> v1{123}, v2{"XYZ"}; print(v1,' '); print(v2); std::swap(v1, v2); print(v1,' '); print(v2); std::variant<double,std::string> v3{3.14};// std::swap(v1, v3); // ERROR: ~ inconsistent parameter packs}
Output:
123 XYZXYZ 123
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 | swap was notconstexpr while the required operations can beconstexpr in C++20 | madeconstexpr |
swaps with anothervariant(public member function)[edit] |