std::is_object
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_object; |
(C++11以降) | |
std::is_object は UnaryTypeTrait です。
T が オブジェクト型 (つまり、関数型、参照型、または void 型以外の、cv修飾されている可能性のある型) である場合、メンバー定数 value は true となります。それ以外の型の場合、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 [static] |
T がオブジェクト型の場合 true、それ以外の場合 false。(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
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) |
型が共用体でないクラス型であるかをチェックする (クラステンプレート) |