std::is_compound
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_compound; |
(C++11 起) | |
std::is_compound
是一個 一元型別特性 (UnaryTypeTrait)。
如果 T
是複合型別(即陣列、函式、物件指標、函式指標、成員物件指標、成員函式指標、引用、類、聯合或列舉,包括任何 cv 限定變體),則提供成員常量 value
等於 true。對於任何其他型別,value
為 false。
如果程式為 std::is_compound
或 std::is_compound_v
新增特化,則行為是未定義的。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool is_compound_v = is_compound<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
如果 T 是複合型別,則為 true,否則為 false(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
std::integral_constant<bool, value> |
[編輯] 注意
複合型別是根據基本型別構建的型別。任何 C++ 型別要麼是基本型別,要麼是複合型別。
[編輯] 可能的實現
template<class T> struct is_compound : std::integral_constant<bool, !std::is_fundamental<T>::value> {}; |
[編輯] 示例
執行此程式碼
#include <type_traits> #include <iostream> static_assert(not std::is_compound_v<int>); static_assert(std::is_compound_v<int*>); static_assert(std::is_compound_v<int&>); void f(); static_assert(std::is_compound_v<decltype(f)>); static_assert(std::is_compound_v<decltype(&f)>); static_assert(std::is_compound_v<char[100]>); class C {}; static_assert(std::is_compound_v<C>); union U {}; static_assert(std::is_compound_v<U>); enum struct E { e }; static_assert(std::is_compound_v<E>); static_assert(std::is_compound_v<decltype(E::e)>); struct S { int i : 8; int j; void foo(); }; static_assert(not std::is_compound_v<decltype(S::i)>); static_assert(not std::is_compound_v<decltype(S::j)>); static_assert(std::is_compound_v<decltype(&S::j)>); static_assert(std::is_compound_v<decltype(&S::foo)>); int main() { std::cout << "All checks have passed\n"; }
[編輯] 另請參閱
(C++11) |
檢查型別是否為基本型別 (類模板) |
(C++11) |
檢查型別是否為標量型別 (類模板) |
(C++11) |
檢查型別是否為物件型別 (類模板) |
(C++11) |
檢查型別是否為陣列型別 (類模板) |