std::experimental::conjunction
From cppreference.com
< cpp | experimental
| ヘッダ <experimental/type_traits> で定義 |
||
| template< class... B > struct conjunction; |
(Library Fundamentals TS v2) | |
型特性 B... の論理積を形成し、実質的に特性のシーケンスに対して論理ANDを実行します。
std::experimental::conjunction<B1, ..., BN> の特殊化は、公開かつ明確な基底クラスを持ちます。
- もし sizeof...(B) == 0 ならば、std::true_type です。そうでなければ、
B1, ..., BNの中で、bool(Bi::value) == false となる最初の型Bi、またはそのような型がない場合はBNとなります。
基底クラスのメンバー(conjunction および operator= を除く)は隠されておらず、conjunction で明確に利用可能です。
Conjunction は短絡評価を行います。つまり、bool(Bi::value) == false となるテンプレート型引数 Bi が存在する場合、conjunction<B1, ..., BN>::value のインスタンス化は、j > i となる Bj::value のインスタンス化を必要としません。
目次 |
[編集] テンプレートパラメータ
| B... | - | Bi::value がインスタンス化されるすべてのテンプレート引数 Bi は、基底クラスとして使用可能であり、bool に変換可能なメンバー value を定義する必要があります。 |
[編集] ヘルパー変数テンプレート
| template< class... B > constexpr bool conjunction_v = conjunction<B...>::value; |
(Library Fundamentals TS v2) | |
[編集] 実装例
template<class...> struct conjunction : std::true_type {}; template<class B1> struct conjunction<B1> : B1 {}; template<class B1, class... Bn> struct conjunction<B1, Bn...> : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {}; |
[編集] 注記
conjunction の特殊化は、必ずしも std::true_type または std::false_type から継承するわけではありません。単に、bool に変換された ::value が false となる最初の B から継承するか、すべて true に変換される場合は最後の B から継承します。たとえば、conjunction<std::integral_constant<int, 2>, std::integral_constant<int, 4>>::value は 4 になります。
[編集] 例
このコードを実行
#include <experimental/type_traits> #include <iostream> // func is enabled if all Ts... have the same type template<typename T, typename... Ts> constexpr std::enable_if_t<std::experimental::conjunction_v<std::is_same<T, Ts>...>> func(T, Ts...) { std::cout << "All types are the same.\n"; } template<typename T, typename... Ts> constexpr std::enable_if_t<!std::experimental::conjunction_v<std::is_same<T, Ts>...>> func(T, Ts...) { std::cout << "Types differ.\n"; } int main() { func(1, 2'7, 3'1); func(1, 2.7, '3'); }
出力
All types are the same. Types differ.
[編集] 関連情報
| (C++17) |
可変長論理ANDメタ関数 (クラステンプレート) |