integral_constant定义编译期常量
来源:互联网 发布:php分页步骤 编辑:程序博客网 时间:2024/06/07 00:07
#include "stdafx.h"
#include <iostream>
#include <type_traits>
using namespace std;
/*
integral_constant就可方便地定义编译期常量,而无需再通过enum和static const变量方式,这也为定义编译期常量提供另一种方法。
integral_constant类 是所有traits类的基类,分别提供了以下功能:
value_type 表示值的类型
value表示值
type 表示自己, 因此可以用::type::value来获取值
true_type和false_type两个特化类用来表示bool值类型的traits,很多traits类都需要继承它们
struct integral_constant
{
// convenient template for integral constant types
static _CONST_DATA _Ty value = _Val;
typedef _Ty value_type;
typedef integral_constant<_Ty, _Val> type;
_CONST_FUN operator value_type() const _NOEXCEPT
{
return (value);// return stored value
}
_CONST_FUN value_type operator()() const _NOEXCEPT
{
return (value);// return stored value
}
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
// ALIAS TEMPLATE bool_constant
template<bool _Val>
using bool_constant = integral_constant<bool, _Val>;
*/
template <unsigned n>
struct factorial : integral_constant<int , n * factorial<n - 1>::value>
{
};
template <>//特化
struct factorial<0> : integral_constant<int, 1>
{
};
int main()
{
typedef integral_constant<int, 111> IC;
cout << "value " << IC::value << endl;
cout << "value_type " << typeid(IC::value_type).name() << endl;
cout << "type " << typeid(IC::type).name() << endl;
IC ic;
int c1 = ic; //调用的operator value_type()
cout << c1 << endl;
int c2 = ic();//调用的 operator()()
cout << c1 << endl;
cout << integral_constant<char, 'a'>::value <<endl;
/*
非型别模版参数必须是整型或者和整型兼容的, double和float不行
cout << integral_constant<double, 12.23>::value << endl;
cout << integral_constant<float, 12.23f>::value << endl;
*/
cout << true_type::value << endl;
cout << bool_constant <false>::value << endl;
cout << factorial<5>::value<<endl; // constexpr (no calculations on runtime)
}
#include <iostream>
#include <type_traits>
using namespace std;
/*
integral_constant就可方便地定义编译期常量,而无需再通过enum和static const变量方式,这也为定义编译期常量提供另一种方法。
integral_constant类 是所有traits类的基类,分别提供了以下功能:
value_type 表示值的类型
value表示值
type 表示自己, 因此可以用::type::value来获取值
true_type和false_type两个特化类用来表示bool值类型的traits,很多traits类都需要继承它们
struct integral_constant
{
// convenient template for integral constant types
static _CONST_DATA _Ty value = _Val;
typedef _Ty value_type;
typedef integral_constant<_Ty, _Val> type;
_CONST_FUN operator value_type() const _NOEXCEPT
{
return (value);// return stored value
}
_CONST_FUN value_type operator()() const _NOEXCEPT
{
return (value);// return stored value
}
};
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
// ALIAS TEMPLATE bool_constant
template<bool _Val>
using bool_constant = integral_constant<bool, _Val>;
*/
template <unsigned n>
struct factorial : integral_constant<int , n * factorial<n - 1>::value>
{
};
template <>//特化
struct factorial<0> : integral_constant<int, 1>
{
};
int main()
{
typedef integral_constant<int, 111> IC;
cout << "value " << IC::value << endl;
cout << "value_type " << typeid(IC::value_type).name() << endl;
cout << "type " << typeid(IC::type).name() << endl;
IC ic;
int c1 = ic; //调用的operator value_type()
cout << c1 << endl;
int c2 = ic();//调用的 operator()()
cout << c1 << endl;
cout << integral_constant<char, 'a'>::value <<endl;
/*
非型别模版参数必须是整型或者和整型兼容的, double和float不行
cout << integral_constant<double, 12.23>::value << endl;
cout << integral_constant<float, 12.23f>::value << endl;
*/
cout << true_type::value << endl;
cout << bool_constant <false>::value << endl;
cout << factorial<5>::value<<endl; // constexpr (no calculations on runtime)
}
阅读全文
0 0
- integral_constant定义编译期常量
- integral_constant定义编译期常量
- Java编译期常量
- 编译期常量
- java 编译期常量
- 编译期常量
- 编译期常量和非编译期常量
- java编译期常量 static final
- 编译期常量的一些事儿
- 将“执行期变量”转成“编译期常量”
- 编译原理_常量定义语句语法分析
- 编译期强制函数参数为字符串常量
- C++利用常量表达式在编译期操作字符串
- 类字面常量 .class 和 forname()方法的区别 编译期常量
- 字符串编译期与运行期分析及字符串常量池
- 常量定义
- 常量定义
- 定义常量
- php链接sqlserver类
- 909422229_Jquery获取元素标签内容与设置
- 如果通过adb查看当前显示的activity
- Sum—LeetCode-445 Add Two Numbers II
- Swift 手动导入百度地图
- integral_constant定义编译期常量
- wdatepicker怎么渲染某日期样式
- UFLDL Tutorial Exercise
- Linux 查看CPU和内存使用情况
- ORACLE用来查询父表的子表
- 【Deep Learning】tensorflow实现卷积神经网络(AlexNet)
- 从头到尾彻底理解傅里叶变换算法(下)
- Python-leaning-StateMachine
- 简单多线程执行任务