C++标准模板(STL)- 类型支持 (类型属性,检查类型是否聚合类型,std::is_aggregate)

类型特性


类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。

类型属性

检查类型是否聚合类型

std::is_aggregate

定义于头文件 <type_traits>

template< class T >
struct is_aggregate;

(C++17 起)

检查 T 是否为聚合类型。若 T 为聚合类型,则成员常量 value 等于 true ,否则为 false 。

若 std::remove_all_extents_t<T> 是异于(可以有 cv 限定的) void 的不完整类型,则行为未定义。

模板形参

T-要检查的类型

辅助变量模板

template< class T >
inline constexpr bool is_aggregate_v = is_aggregate<T>::value;

(C++17 起)

继承自 std::integral_constant

成员常量

value

[静态]

T为聚合类型则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool

转换对象为 bool ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typebool
typestd::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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值