C++标准模板(STL)- 类型支持 (辅助类,具有指定值的指定类型的编译期常量)

类型特性

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

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

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

辅助类

std::integral_constant

定义于头文件 <type_traits>

integral_constant  (C++11)

bool_constant       (C++17)

具有指定值的指定类型的编译期常量
(类模板)

标准提供 std::integral_constant 对类型 bool 的二个特化:

定义于头文件 <type_traits>

类型定义
true_typestd::integral_constant<bool, true>
false_typestd::integral_constant<bool, false>

template< class T, T v >
struct integral_constant;

(C++11 起)

std::integral_constant 包装特定类型的静态常量。它是 C++ 类型特性的基类。

帮助模板

帮助别名模板 std::bool_constantT 为 bool 的常用情况定义。

template <bool B>
using bool_constant = integral_constant<bool, B>;

(C++17 起)

为其中 T 为 bool 的二种常用情形提供 typedef :

定义于头文件 <type_traits>

类型定义
true_typestd::integral_constant<bool, true>
false_typestd::integral_constant<bool, false>

成员类型

类型定义
value_typeT
typestd::integral_constant<T,v>

成员常量

名称

constexpr T value

[静态]

T 类型的值为 v 的静态常量
(公开静态成员常量)

成员函数

operator value_type

返回包装的值
(公开成员函数)

operator()

(C++14)

返回包装的值
(公开成员函数)

std::integral_constant::operator value_type

constexpr operator value_type() const noexcept;

转换函数。返回包装的值。

std::integral_constant::operator()

constexpr value_type operator()() const noexcept;

(C++14 起)

返回包装的值。此函数允许 std::integral_constant 被用作编译时函数对象的源。

可行实现

template<class T, T v>
struct integral_constant {
    static constexpr T value = v;
    typedef T value_type;
    typedef integral_constant type; // 使用注入的类名
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; } // c++14 起
};

调用示例

#include <iostream>
#include <type_traits>

int main()
{
    std::cout << std::boolalpha;

    typedef std::integral_constant<int, 2> two_t;
    typedef std::integral_constant<int, 4> four_t;

    std::cout << "std::is_same<two_t, four_t>::value:       "
              << std::is_same<two_t, four_t>::value << std::endl;

    std::cout << "two_t::value * 2 == four_t::value:        "
              << (two_t::value * 2 == four_t::value) << std::endl;


    enum class my_e
    {
        e1,
        e2
    };
    typedef std::integral_constant<my_e, my_e::e1> my_e_e1;
    typedef std::integral_constant<my_e, my_e::e2> my_e_e2;

    std::cout << "my_e_e1::value == my_e::e2:               "
              << (my_e_e1::value == my_e::e2) << std::endl;

    std::cout << "std::is_same<my_e_e2, my_e_e2>::value:    "
              << std::is_same<my_e_e2, my_e_e2>::value << std::endl;

    return 0;
}

输出

std::is_same<two_t, four_t>::value:       false
two_t::value * 2 == four_t::value:        true
my_e_e1::value == my_e::e2:               false
std::is_same<my_e_e2, my_e_e2>::value:    true

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值