std::pair<T1,T2>::swap
void swap( pair& other ) noexcept(/* see below */);
|
(1) | (seit C++11) (constexpr seit C++20) |
constexpr void swap( const pair& other ) const noexcept(/* see below */);
|
(2) | (seit C++23) |
Tauscht first mit other.first und second mit other.second, als ob durch using std::swap; swap(first, other.first); swap(second, other.second);.
|
Falls einer der ausgewählten |
(bis C++23) |
|
1) Das Programm ist fehlerhaft, wenn entweder
std::is_swappable_v<T1> oder std::is_swappable_v<T2> nicht true ist.2) Das Programm ist fehlerhaft, wenn entweder
std::is_swappable_v<const T1> oder std::is_swappable_v<const T2> nicht true ist.Falls einer der ausgewählten |
(seit C++23) |
Parameter
| other | - | Paar von Werten zum Austauschen |
Ausnahmen
|
noexcept
Spezifikation:
noexcept
(
noexcept
(
swap
(
first, other.
first
)
)
&&
Im obigen Ausdruck wird der Bezeichner
|
(bis C++17) |
|
1)
noexcept
Spezifikation:
noexcept
(
std::
is_nothrow_swappable_v
<
first_type
>
&&
2)
noexcept
Spezifikation:
noexcept
(
std::
is_nothrow_swappable_v
<
const
first_type
>
&&
|
(seit C++17) |
Beispiel
#include <iostream> #include <utility> #include <string> int main() { std::pair<int, std::string> p1(10, "test"), p2; p2.swap(p1); std::cout << "(" << p2.first << ", " << p2.second << ")\n"; #if __cpp_lib_ranges_zip >= 202110L // Verwendung der C++23 const-qualifizierten Swap-Überladung // (swap propagiert nicht länger die Konstanz von pair) int i1 = 10, i2{}; std::string s1("test"), s2; const std::pair<int&, std::string&> r1(i1, s1), r2(i2, s2); r2.swap(r1); std::cout << "(" << i2 << ", " << s2 << ")\n"; #endif }
Mögliche Ausgabe:
(10, test) (10, test)
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrektes Verhalten |
|---|---|---|---|
| LWG 2456 | C++11 | die noexcept Spezifikation ist fehlerhaft
|
funktionsfähig gemacht |
Siehe auch
|
tauscht die Werte zweier Objekte
(Funktions-Template) |
|
tauscht die Inhalte zweier
tuple
s
(öffentliche Elementfunktion von
std::tuple<Types...>
)
|