GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::variant::swap(3) C++ Standard Libary std::variant::swap(3)

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


void swap( variant& rhs ) noexcept(/* see below */); (since C++17)
(until C++20)
constexpr void swap( variant& rhs ) noexcept(/* see below */); (since C++20)


Swaps two variant objects.


* if both *this and rhs are valueless by exception, does nothing
* otherwise, if both *this and rhs hold the same alternative, calls
swap(std::get<i>(*this), std::get<i>(rhs)) where i is index(). If an exception
is thrown, the state of the values depends on the exception safety of the swap
function called.
* otherwise, exchanges values of rhs and *this. If an exception is thrown, the
state of *this and rhs depends on exception safety of variant's move
constructor.


The behavior is undefined unless lvalues of type T_i are Swappable and
std::is_move_constructible_v<T_i> is true for all T_i in Types....


rhs - a variant object to swap with


(none)


If this->index() == rhs.index(), may throw any exception thrown by
swap(std::get<i>(*this), std::get<i>(rhs)) with i being index().


Otherwise, may throw any exception thrown by the move constructors of the
alternatives currently held by *this and rhs.


noexcept specification:
noexcept(((std::is_nothrow_move_constructible_v<Types> &&
std::is_nothrow_swappable_v<Types>) && ...))

// Run this code


#include <variant>
#include <string>
#include <iostream>


int main()
{
std::variant<int, std::string> v1{2}, v2{"abc"};
std::visit([] (auto&& x) { std::cout << x << ' '; }, v1);
std::visit([] (auto&& x) { std::cout << x << '\n'; }, v2);
v1.swap(v2);
std::visit([] (auto&& x) { std::cout << x << ' '; }, v1);
std::visit([] (auto&& x) { std::cout << x << '\n'; }, v2);
}


2 abc
abc 2


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 swap was not constexpr while the required made constexpr
operations can be constexpr in C++20

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.