std::experimental::conjunction
來自 cppreference.com
定義於標頭檔案 <experimental/type_traits> |
||
template< class... B > struct conjunction; |
(庫基礎 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 是短路的:如果存在模板型別引數 Bi
,使得 bool(Bi::value) == false,則例項化 conjunction<B1, ..., BN>::value 不需要例項化 Bj::value,其中 j > i。
目錄 |
[編輯] 模板引數
B... | - | 每個模板引數 Bi (其 Bi::value 被例項化)都必須可用作基類,並定義可轉換為 bool 的成員 value |
[編輯] 輔助變數模板
template< class... B > constexpr bool conjunction_v = conjunction<B...>::value; |
(庫基礎 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:它僅繼承自第一個其 ::value 轉換為 bool 為 false 的 B,或者當所有 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 元函式 (類模板) |