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

履歴 編集

class template
<type_traits>

std::negation(C++17)

namespace std {
  template <class Trait>
  struct negation;

  template <class Trait>
  inline constexpr bool negation_v = negation<Trait>::value;
}

概要

特性(bool値を返すメタ関数)の論理否定を計算する。

要件

型引数Traitboolに変換可能なメンバ変数valueを持つこと。

効果

bool_constant<!Trait::value>から派生する。

すなわち、Trait::value == trueならばfalse_typeから派生し、Trait::value == falseならばtrue_typeから派生する。

主にconjunctiondisjunctionのような、結果となる::valueではなくメタ関数そのものを受け取る高階メタ関数の引数において結果の真理値を反転させたいときに利用する。

#include <type_traits>
#include <iostream>
#include <memory>

template<typename T>
void f() {
  std::cout << std::boolalpha << "T::value = " << T::value << std::endl;
}

//型Tがムーブ構築のみが可能かどうかを調べる(is_move_constructible == true && is_copy_constructible == false)
template<typename T>
using is_only_move = std::conjunction<std::is_move_constructible<T>, std::negation<std::is_copy_constructible<T>>>;

int main()
{
  f<std::true_type>();
  f<std::negation<std::true_type>>();

  f<is_only_move<std::unique_ptr<int>>>();
}

出力

T::value = true
T::value = false
T::value = true

実装例

template <class Trait>
struct negation : bool_constant<!Trait::value> {};

バージョン

言語

  • C++17

処理系

  • Clang: 3.8
  • GCC: 6.3
  • Visual C++: 2015 update2, 2017
    • negation_vは、2015ではインテリセンスからは見えないが利用可能である。

参照