Namespaces
Variants

std:: is_object

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
is_object
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <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
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
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)
检查类型是否为非联合体的类类型
(类模板)