根据数字参数更改模板成员
Change template members based on numeric parameters?
我希望能够完成这样的事情:
template<int size>
struct myStruct
{
(size > 5 ? int64_t : int32_t) value;
};
一种方法是对每一套可能的价值观进行明确的专业化,但这显然并不理想。有人知道更好的方法吗?
使用std::conditional
。这需要C++11,但你可以很容易地编写自己的:
template<int size>
struct myStruct
{
typename std::conditional<(size > 5), int64_t, int32_t>::type
value;
};
或者在C++14中:
template<int size>
struct myStruct
{
std::conditional_t<(size > 5), int64_t, int32_t> value;
};
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 检查输入是否不是整数或数字
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 从C 到C#调用INT []显示随机大数字,而不是原始数组成员
- 成员初始化列表中初始化的向量与传递给构造函数的数字不同
- 使用default_random_engine作为类成员,而没有类的情况下会产生不同的数字(同一种子)
- 为具有一个数字数据成员的类定义所有比较运算符的方便方法
- 如何从 char 数组中提取数字字符串成员
- 初始化成员函数字段
- 将std::string转换为任意数字类型或std::string的成员函数模板
- 根据数字参数更改模板成员