std::is_object
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_object; |
(C++11 起) | |
std::is_object
是一個 UnaryTypeTrait。
如果 T
是一個物件型別(即任何可能帶 cv 限定符的型別,而非函式、引用或 void 型別),則提供等於 true 的成員常量 value
。對於任何其他型別,value
為 false。
如果程式為 std::is_object
或 std::is_object_v
新增特化,則行為未定義。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool is_object_v = is_object<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> |
[編輯] 可能的實現
template<class T> struct is_object : std::integral_constant<bool, std::is_scalar<T>::value || std::is_array<T>::value || std::is_union<T>::value || std::is_class<T>::value> {}; |
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <type_traits> #define IS_OBJECT(...) \ std::cout << std::boolalpha << std::left << std::setw(9) << #__VA_ARGS__ \ << (std::is_object_v<__VA_ARGS__> ? " is object\n" \ : " is not an object\n") int main() { class cls {}; IS_OBJECT(void); IS_OBJECT(int); IS_OBJECT(int&); IS_OBJECT(int*); IS_OBJECT(int*&); IS_OBJECT(cls); IS_OBJECT(cls&); IS_OBJECT(cls*); IS_OBJECT(int()); IS_OBJECT(int(*)()); IS_OBJECT(int(&)()); }
輸出
void is not an object int is object int& is not an object int* is object int*& is not an object cls is object cls& is not an object cls* is object int() is not an object int(*)() is object int(&)() is not an object
[編輯] 參閱
(C++11) |
檢查型別是否為標量型別 (類模板) |
(C++11) |
檢查型別是否為陣列型別 (類模板) |
(C++11) |
檢查型別是否為聯合型別 (類模板) |
(C++11) |
檢查型別是否為非聯合類型別 (類模板) |