C++标准模板(STL)- 类型支持 (类型属性,is_literal_type,is_polymorphic,is_empty)

类型特性

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

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

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

类型属性

定义于头文件 <type_traits>

继承自 std::integral_constant

成员常量

|--------------|------------------------------------------|
| value [静态] | 若 T 为字面类型则为 true ,否则为 false (公开静态成员常量) |

成员函数

|--------------------|---------------------------------|
| operator bool | 转换对象为 bool ,返回 value (公开成员函数) |
| operator() (C++14) | 返回 value (公开成员函数) |

成员类型

|--------------|---------------------------------------|
| 类型 | 定义 |
| value_type | bool |
| type | std::integral_constant<bool, value> |

检查类型是否为字面类型

std::is_literal_type

|-----------------------------------------------|---|-----------------------------------|
| template< class T > struct is_literal_type; | | (C++11 起) (C++17 中弃用) (C++20 中移除) |

T 满足所有字面类型 (LiteralType) 的要求,则提供等于 true 的成员常量 value 。对于任何其他类型, value 为 false 。

若 std::remove_all_extents_t<T> 为不完整类型且非(可有 cv 限定的) void 则行为未定义。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|----------------------------------------------------------------------------------------------|---|----------------------------|
| template< class T > inline constexpr bool is_literal_type_v = is_literal_type<T>::value; | | (C++17 起) (弃用) (C++20 中移除) |

注意

唯有字面类型能作为 constexpr 函数的参数或返回类型。只有字面类能拥有 constexpr 成员函数。

调用示例
#include <iostream>
#include <string>
#include <type_traits>

struct A
{
    int m;
};

struct B
{
    virtual ~B();
};

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_literal_type<int>::value:             "
              << std::is_literal_type<int>::value << std::endl;
    std::cout << "std::is_literal_type<double>::value:          "
              << std::is_literal_type<double>::value << std::endl;
    std::cout << "std::is_literal_type<std::string>::value:     "
              << std::is_literal_type<std::string>::value << std::endl;
    std::cout << "std::is_literal_type<A>::value:               "
              << std::is_literal_type<A>::value << std::endl;
    std::cout << "std::is_literal_type<B>::value:               "
              << std::is_literal_type<B>::value << std::endl;

    return 0;
}
输出

检查类型是否为类(但非联合体)类型且无非静态数据成员

std::is_empty

|----------------------------------------|---|-----------|
| template< class T > struct is_empty; | | (C++11 起) |

T 是空类类型(即无异于 0 大小位域的非静态数据成员、无虚函数、无虚基类,且无非空基类的非联合类类型),则提供等于 true 的成员常量 value 。对于任何其他类型, value 为 false 。

T 是非联合类类型,则 T 应是完整类型;否则行为未定义。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|--------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_empty_v = is_empty<T>::value; | | (C++17 起) |

注意

因为空基类优化,从空类类型继承通常不增加类的大小。

std::is_empty<T> 及所有其他类型特性均为空类。

调用示例
#include <iostream>
#include <type_traits>
#include <string>

struct A {};

struct B
{
    int m;
};

struct C
{
    virtual ~C();
};

union D {};

struct E
{
    [[no_unique_address]] D d;
};

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_empty<int>::value:            "
              << std::is_empty<int>::value << std::endl;
    std::cout << "std::is_empty<double>::value:         "
              << std::is_empty<double>::value << std::endl;
    std::cout << "std::is_empty<std::string>::value:    "
              << std::is_empty<std::string>::value << std::endl;
    std::cout << "std::is_empty<A>::value:              "
              << std::is_empty<A>::value << std::endl;
    std::cout << "std::is_empty<B>::value:              "
              << std::is_empty<B>::value << std::endl;
    std::cout << "std::is_empty<C>::value:              "
              << std::is_empty<C>::value << std::endl;
    std::cout << "std::is_empty<D>::value:              "
              << std::is_empty<D>::value << std::endl;
    std::cout << "std::is_empty<E>::value:              "
              << std::is_empty<E>::value << std::endl; // 结果依赖于 ABI

    return 0;
}
输出

检查类型是否为多态类类型

std::is_polymorphic

|----------------------------------------------|---|-----------|
| template< class T > struct is_polymorphic; | | (C++11 起) |

T 为多态类(即声明或继承至少一个虚函数的非联合类),则提供等于 true 的成员常量 value 。对于任何其他类型, value 为 false 。

T 是非联合类类型,则 T 应为完整类型;否则行为未定义。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|--------------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_polymorphic_v = is_polymorphic<T>::value; | | (C++17 起) |

可能的实现
namespace detail {
 
template <class T>
std::true_type detect_is_polymorphic(
decltype(dynamic_cast<const volatile void*>(static_cast<T*>(nullptr)))
);
template <class T>
std::false_type detect_is_polymorphic(...);
} // namespace detail
 
template <class T>
struct is_polymorphic : 
decltype(detail::detect_is_polymorphic<T>(nullptr)) {};
调用示例
#include <iostream>
#include <type_traits>

struct A
{
    int m;
};

struct B
{
    virtual void foo();
};

struct C : B {};

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_polymorphic<int>::value:              "
              << std::is_polymorphic<int>::value << std::endl;
    std::cout << "std::is_polymorphic<double>::value:           "
              << std::is_polymorphic<double>::value << std::endl;
    std::cout << "std::is_polymorphic<std::string>::value:      "
              << std::is_polymorphic<std::string>::value << std::endl;
    std::cout << "std::is_polymorphic<A>::value:                "
              << std::is_polymorphic<A>::value << std::endl;
    std::cout << "std::is_polymorphic<B>::value:                "
              << std::is_polymorphic<B>::value << std::endl;
    std::cout << "std::is_polymorphic<C>::value:                "
              << std::is_polymorphic<C>::value << std::endl;

    return 0;
}
输出
相关推荐
努力学习的小廉18 分钟前
【C++】 —— 笔试刷题day_4
开发语言·c++
努力学习的小廉1 小时前
【C++】 —— 笔试刷题day_3
开发语言·c++
@@永恒3 小时前
红黑树介绍
数据结构·c++
头发尚存的猿小二3 小时前
2024年第十五届蓝桥杯软件C/C++大学A组——五子棋对弈
c语言·c++·蓝桥杯
*TQK*4 小时前
双指针算法介绍+算法练习(2025)
c++·学习·算法·双指针
掘了4 小时前
C++ 程序员应该了解的 Linux 命令
linux·c++·后端
郭涤生4 小时前
并行算法_第十章_《C++并发编程实战》笔记
c++·算法·并发编程
newki5 小时前
学习笔记,C/C++编译相关概念与相关示例
android·c++·c
UpUpUp……5 小时前
Linux操作系统下Git的使用详细步骤
linux·运维·c++·git
橘子真甜~6 小时前
36.C++二叉树进阶5(平衡二叉搜索树 - 红黑树及其插入操作图解)
开发语言·数据结构·c++·算法·面试·二叉树·红黑树