名前空間
変種
操作

std::exchange

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

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)  
(C++20)
スワップ型操作
exchange
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
ヘッダ <utility> で定義
template< class T, class U = T >
T exchange( T& obj, U&& new_value );
(C++14以降)
(C++20 以降 constexpr)
(C++23 以降、条件付き noexcept)

obj の値を new_value で置き換え、obj の古い値を返します。

目次

[編集] パラメーター

obj - 値を置き換えるオブジェクト
new_value - obj に代入する値
型要件
-
TMoveConstructible の要件を満たしている必要があります。また、U 型のオブジェクトを T 型のオブジェクトにムーブ代入できる必要があります。

[編集] 戻り値

obj の古い値。

[編集] 例外

(なし)

(C++23まで)
noexcept 指定:  
(C++23から)

[編集] 実装例

template<class T, class U = T>
constexpr // Since C++20
T exchange(T& obj, U&& new_value)
    noexcept( // Since C++23
        std::is_nothrow_move_constructible<T>::value &&
        std::is_nothrow_assignable<T&, U>::value
    )
{
    T old_value = std::move(obj);
    obj = std::forward<U>(new_value);
    return old_value;
}

[編集] 備考

std::exchange は、ムーブコンストラクタの実装や、特別なクリーンアップを必要としないメンバのムーブ代入演算子の実装に使用できます。

struct S
{
    int n;
 
    S(S&& other) noexcept : n{std::exchange(other.n, 0)} {}
 
    S& operator=(S&& other) noexcept
    {
        n = std::exchange(other.n, 0); // Move n, while leaving zero in other.n
        // Note: in case of self-move-assignment, n is unchanged
        // Also note: if n is an opaque resource handle that requires
        //            special cleanup, the resource is leaked.
        return *this;
    }
};
機能テストマクロ 規格 機能
__cpp_lib_exchange_function 201304L (C++14) std::exchange

[編集]

#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
 
class stream
{
public:
    using flags_type = int;
 
public:
    flags_type flags() const { return flags_; }
 
    // Replaces flags_ by newf, and returns the old value.
    flags_type flags(flags_type newf) { return std::exchange(flags_, newf); }
 
private:
    flags_type flags_ = 0;
};
 
void f() { std::cout << "f()"; }
 
int main()
{
    stream s;
 
    std::cout << s.flags() << '\n';
    std::cout << s.flags(12) << '\n';
    std::cout << s.flags() << "\n\n";
 
    std::vector<int> v;
 
    // Since the second template parameter has a default value, it is possible
    // to use a braced-init-list as second argument. The expression below
    // is equivalent to std::exchange(v, std::vector<int>{1, 2, 3, 4});
 
    std::exchange(v, {1, 2, 3, 4});
 
    std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, ", "));
 
    std::cout << "\n\n";
 
    void (*fun)();
 
    // The default value of template parameter also makes possible to use a
    // normal function as second argument. The expression below is equivalent to
    // std::exchange(fun, static_cast<void(*)()>(f))
    std::exchange(fun, f);
    fun();
 
    std::cout << "\n\nFibonacci sequence: ";
    for (int a{0}, b{1}; a < 100; a = std::exchange(b, a + b))
        std::cout << a << ", ";
    std::cout << "...\n";
}

出力

0
0
12
 
1, 2, 3, 4,
 
f()
 
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

[編集] 関連項目

2つのオブジェクトの値を交換する
(関数テンプレート) [編集]
アトミックオブジェクトの値を非アトミックな引数でアトミックに置き換え、アトミックオブジェクトの古い値を返す
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)