名前空間
変種
操作

std::tuple<Types...>::swap

From cppreference.com
< cpp‎ | utility‎ | tuple
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダ <tuple> で定義
void swap( tuple& other ) noexcept(/* see below */);
(1) (C++11以降)
(C++20 以降 constexpr)
constexpr void swap( const tuple& other ) noexcept(/* see below */) const;
(2) (C++23から)

swap(これはstd::swapである可能性も、ADLによって見つかる可能性もあります)を、*thisの各要素とotherの対応する要素に対して呼び出します。

選択されたswap関数の呼び出しのいずれかが不正形式である場合、または両方のタプルの対応する要素を交換しない場合、動作は未定義です。

(C++23まで)

選択されたswap関数の呼び出しのいずれかが両方のタプルの対応する要素を交換しない場合、動作は未定義です。

1) (std::is_swappable_v<Types> && ...)true でない場合、プログラムは不正形式となります。
2) (std::is_swappable_v<const Types> && ...)true でない場合、プログラムは不正形式となります。
(C++23から)

目次

[edit] Parameters

その他 - 交換する値のタプル

[edit] Return value

(なし)

[edit] Exceptions

noexcept 指定:  
noexcept(

    noexcept(swap(std::declval<T0&>>(), std::declval<T0&>())) &&
    noexcept(swap(std::declval<T1&>>(), std::declval<T1&>())) &&
    noexcept(swap(std::declval<T2&>>(), std::declval<T2&>())) &&
    ...

)

上記の式において、識別子swapは、C++17のstd::is_nothrow_swappableトレイトが使用するものと同じ方法で検索されます。

(C++17まで)
1)
noexcept 指定:  
noexcept((std::is_nothrow_swappable_v<Types> && ...))
2)
noexcept 指定:  
noexcept((std::is_nothrow_swappable_v<const Types> && ...))
(C++17以降)

[edit] Example

#include <iostream>
#include <string>
#include <tuple>
 
int main()
{
    std::tuple<int, std::string, float> p1{42, "ABCD", 2.71}, p2;
    p2 = std::make_tuple(10, "1234", 3.14);
 
    auto print_p1_p2 = [&](auto rem)
    {
        std::cout << rem
                  << "p1 = {" << std::get<0>(p1)
                  << ", "     << std::get<1>(p1)
                  << ", "     << std::get<2>(p1) << "}, "
                  << "p2 = {" << std::get<0>(p2)
                  << ", "     << std::get<1>(p2)
                  << ", "     << std::get<2>(p2) << "}\n";
    };
 
    print_p1_p2("Before p1.swap(p2): ");
    p1.swap(p2);
    print_p1_p2("After  p1.swap(p2): ");
    swap(p1, p2);
    print_p1_p2("After swap(p1, p2): ");
}

出力

Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
After  p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71}
After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}

[edit] Defect reports

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2456 C++11 noexcept指定が不正形式 動作するように修正

[edit] See also

std::swap アルゴリズムを特殊化する
(関数テンプレート) [編集]
(C++11)
内容を交換する
(std::pair<T1,T2>のpublicメンバ関数) [edit]
English 日本語 中文(简体) 中文(繁體)