类型特性
类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。
定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
类型属性
检查类型是否聚合类型
std::is_aggregate
定义于头文件 | ||
template< class T > | (C++17 起) |
检查 T
是否为聚合类型。若 T
为聚合类型,则成员常量 value
等于 true ,否则为 false 。
若 std::remove_all_extents_t<T> 是异于(可以有 cv 限定的) void 的不完整类型,则行为未定义。
模板形参
T | - | 要检查的类型 |
辅助变量模板
template< class T > | (C++17 起) |
继承自 std::integral_constant
成员常量
value [静态] | 若 T 为聚合类型则为 true ,否则为 false(公开静态成员常量) |
成员函数
operator bool | 转换对象为 bool ,返回 value (公开成员函数) |
operator() (C++14) | 返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type | bool |
type | std::integral_constant<bool, value> |
调用示例
#include <type_traits>
#include <iostream>
// 辅助特征,检测是否有私有或保护的非静态成员
template <typename T, typename = void>
struct has_private_or_protected_non_static_members : std::false_type {};
template <typename T>
struct has_private_or_protected_non_static_members < T,
typename std::enable_if <
!std::is_same<typename std::remove_all_extents<T>::type, T>::value
>::type
>
{
template <typename U> static std::true_type check(int U::*);
template <typename U> static std::false_type check(...);
typedef decltype(check<T>(0)) type;
static const bool value = type::value;
};
// 辅助特征,检测是否有虚函数
template <typename T, typename = void>
struct has_virtual_functions : std::false_type {};
template <typename T>
struct has_virtual_functions < T, typename std::enable_if <
!std::is_same<typename std::remove_all_extents<T>::type, T>::value
>::type >
{
template <typename U> static std::true_type check(int U::*);
template <typename U> static std::false_type check(...);
typedef decltype(check<T>(&T::operator new)) type;
static const bool value = type::value;
};
// 辅助特征,检测是否有虚基类
template <typename T, typename = void>
struct has_virtual_base_classes : std::false_type {};
template <typename T>
struct has_virtual_base_classes < T, typename std::enable_if <
!std::is_same<typename std::remove_all_extents<T>::type, T>::value
>::type >
{
template <typename U> static std::true_type check(int U::*);
template <typename U> static std::false_type check(...);
typedef decltype(check<T>(&T::~T)) type;
static const bool value = type::value;
};
namespace std
{
template <typename T>
struct is_aggregate : std::integral_constant < bool,
std::is_constructible<T>::value &&
std::is_trivially_default_constructible<T>::value &&
!std::is_polymorphic<T>::value &&
!std::is_abstract<T>::value &&
!has_private_or_protected_non_static_members<T>::value &&
!has_virtual_functions<T>::value &&
!has_virtual_base_classes<T>::value
> {};
}
// 使用示例
// 聚合类型
struct A
{
int a;
};
// 聚合类型
struct B
{
B() = default;
~B() = default;
};
// 非聚合类型,有用户定义的构造函数
struct C
{
C() {} ~C() = default;
};
int main()
{
std::cout << std::boolalpha;
std::cout << "A is aggregate: " << std::is_aggregate<A>::value << std::endl;
std::cout << "B is aggregate: " << std::is_aggregate<B>::value << std::endl;
std::cout << "C is aggregate: " << std::is_aggregate<C>::value << std::endl;
return 0;
}
输出
A is aggregate: true
B is aggregate: true
C is aggregate: false