C++标准模板(STL)- 类型支持 (类型特性,is_union,is_class,is_function)

类型特性


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

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

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

类型属性


定义于头文件

基础类型类别

继承自 std::integral_constant

成员常量

value

[静态]

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

operator bool

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

operator()

(C++14)

返回 value
(公开成员函数)
成员类型
类型 定义
value_type bool
type std::integral_constant

检查类型是否为联合体类型

std::is_union

template< class T >
struct is_union;

(C++11 起)

检查 T 是否为联合体类型。若 T 为联合体类型,则提供等于 true 的成员常量 value 。否则 value ,等于 false 。

模板形参
T - 要检查的类型
辅助变量模板

template< class T >
inline constexpr bool is_union_v = is_union::value;

(C++17 起)

检查类型是否非联合类类型

std::is_class

template< class T >
struct is_class;

(C++11 起)

检查 T 是否为非联合类类型。若 T 为类类型(但非联合体),则提供等于 true 的成员常量 value 。否则, value 等于 false 。

注意以 struct 关键词声明的类型是类,故此特性对“结构体”为 true 。

模板形参
T - 要检查的类型
辅助变量模板

template< class T >
inline constexpr bool is_class_v = is_class::value;

 可能的实现
namespace detail {
    template  char test(int T::*);
    struct two { char c[2]; };
    template  two test(...);
}
 
template 
struct is_class : std::integral_constant(0))==1
                                            && !std::is_union::value> {};

检查是否为函数类型

std::is_function

template< class T >
struct is_function;

(C++11 起)

检查 T 是否为函数类型。如 std::function 、 lambda 、有重载 operator() 的类和指向函数指针不是函数类型。若 T 为函数类型,则提供等于 true 的成员常量 value 。否则, value 等于 false 。

模板形参
T - 要检查的类型
辅助变量模板

template< class T >
inline constexpr bool is_function_v = is_function::value;

(C++17 起)
 可能的实现
// 初等模板
template
struct is_function : std::false_type { };
 
// 对常规函数的特化
template
struct is_function : std::true_type {};
 
// 对变参数函数,如 std::printf 的特化
template
struct is_function : std::true_type {};
 
// 对拥有 cv 限定符的函数类型的特化
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
 
// 对拥有引用限定符的函数类型的特化
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
 
// 对所有以上的 noexcept 版本的特化( C++17 及之后)
 
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};
template
struct is_function : std::true_type {};

调用示例

#include 
#include 

struct A {};

typedef union
{
    int a;
    float b;
} B;

struct C
{
    B d;
};

enum E {};

enum class Ec : int {};

struct D
{
    int fun() const&;
};

template
struct PM_traits {};

template
struct PM_traits
{
    using member_type = U;
};

int f();

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_union::value:              "
              << std::is_union::value << std::endl;
    std::cout << "std::is_union::value:              "
              << std::is_union::value << std::endl;
    std::cout << "std::is_union::value:              "
              << std::is_union::value << std::endl;
    std::cout << "std::is_union::value:            "
              << std::is_union::value << std::endl;
    std::cout << "std::is_union::value:          "
              << std::is_union::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_class::value:              "
              << std::is_class::value << std::endl;
    std::cout << "std::is_class::value:              "
              << std::is_class::value << std::endl;
    std::cout << "std::is_class::value:              "
              << std::is_class::value << std::endl;
    std::cout << "std::is_class::value:            "
              << std::is_class::value << std::endl;
    std::cout << "std::is_class::value:          "
              << std::is_class::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_function::value:           "
              << std::is_function::value << std::endl;
    std::cout << "std::is_function::value:    "
              << std::is_function::value << std::endl;
    std::cout << "std::is_function::value: "
              << std::is_function::value << std::endl;
    std::cout << "std::is_function::value:         "
              << std::is_function::value << std::endl;

    using T = PM_traits::member_type; // T 为 int() const&
    std::cout << "std::is_function::value:           "
              << std::is_function::value << std::endl;
    std::cout << std::endl;

    return 0;
}
输出

C++标准模板(STL)- 类型支持 (类型特性,is_union,is_class,is_function)_第1张图片

你可能感兴趣的:(C++标准库模板(STL)-,类型支持,c++,标准库模板,运行时类型识别,is_function,is_union,is_class,基础类型类别)