最終更新日時(UTC):
が更新

履歴 編集

class template
<type_traits>

std::is_nothrow_swappable_with(C++17)

namespace std {
  template <class T, class U>
  struct is_nothrow_swappable_with;

  template <class T, class U>
  inline constexpr bool is_nothrow_swappable_with_v
    = std::is_nothrow_swappable_with<T, U>::value;
}

概要

TUの値が、swap関数によって入れ替え可能であり、その際に例外を投げないかどうかを調べる。

要件

TUが、完全型であること。もしくはconst/volatile修飾された(あるいはされていない)voidか、要素数不明の配列型であること。

効果

TUの間でstd::swappable_with要件を満たしており、2つのswap関数が共に例外を投げない場合はtrue_typeから派生し、そうでなければfalse_typeから派生する。

TUは参照でなければswapできないのでTUが参照でない場合、結果はfalseとなる。

備考

このメタ関数はTUについてのswap関数の直接のコンテキストの妥当性(そのシグネチャで有効なswapがあるかどうか)のみをチェックする。そのため、結果がtrueとなったとしてもswap関数の呼び出しができることは保証されない(その他の要因によりコンパイルエラーが発生する可能性がある)。

また同様に、結果がtrueとなっても見つかったswap関数がswap動作をするかどうかも保証しない。

#include <type_traits>
#include <iostream>

struct spurious_swappable {};

void swap(spurious_swappable&, int&) noexcept {}
void swap(int&, spurious_swappable&) {}

int main()
{
  std::cout << std::boolalpha;

  std::cout << std::is_nothrow_swappable_with<int&, int&>::value << std::endl;

  //参照でなければならない
  std::cout << std::is_nothrow_swappable_with<int, int>::value << std::endl;

  //spurious_swappableは見かけ上はintとswap可能だが、片方のswap関数にnoexceptが無いためnothrow_swappable_withはfalseとなる
  std::cout << std::is_swappable_with<spurious_swappable&, int&>::value << std::endl;
  std::cout << std::is_nothrow_swappable_with<spurious_swappable&, int&>::value << std::endl;
}

出力

true
false
true
false

バージョン

言語

  • C++17

処理系

  • Clang: ??
  • GCC: ??
  • Visual C++: 2015 update3, 2017
    • 2015 update3では、インテリセンスで表示されないが変数テンプレート共々定義されており利用可能である

参照