c++ - 在类模板中使用条件运算符初始化静态 constexpr char 数组成员
问题描述
考虑一个最小的例子
#include <iostream>
template<typename T>
struct foo
{
// won't compile. how to change?
static constexpr char sep[3] = std::is_integral<T>::value ? ". " : ", ";
// many other things ...
};
int main()
{
std::cout << foo<int>::sep << std::endl; // prints .
std::cout << foo<double>::sep << std::endl; // prints ,
}
我想要实现的是:
- 如果
T
具有整数类型,则sep
初始化为.
- 否则,
sep
初始化为,
但是,编译器不允许这样做,说
error: array must be initialized with a brace-enclosed initializer
看起来必须在编译时完成一些事情。但我不知道该怎么做。
我的问题是:我能做些什么来达到这个目的吗?
注意:欢迎进行最小的更改。里面应该还有很多其他的东西foo
。另一个考虑因素是,如果可能的话,我想将所有内容都保留foo
在标头中,并且在源文件中不留下任何内容。
非常感谢。
解决方案
C 数组是不可复制的,所以你必须解决这个问题
检查每个字符:
constexpr char sep[3] = { std::is_integral<T>::value ? '.' : ',', ' ', '\0' };
不要使用数组,而是使用指针(所以你会丢失大小):
constexpr const char* sep = std::is_integral<T>::value ? ". " : ", ";
使用
std::array
:constexpr std::array<char, 3> sep = std::is_integral<T>::value ? std::array<char, 3>{{'.', ' ', 0}} : std::array<char, 3>{{',', ' ', 0}};
使用对数组的引用:
constexpr char dot_sep[3] = std::is_integral<T>::value ? ". " : ", "; constexpr char comma_sep[3] = std::is_integral<T>::value ? ". " : ", "; constexpr const char (&sep)[3] = std::is_integral<T>::value ? dot_sep : comma_sep;
并提供ODR 使用的
dot_sep
/的定义。comma_sep
推荐阅读
- python - Pandas 根据用户活动创建漏斗
- javascript - Mysql 的 Javascript 异步行为
- c# - 如何避免 C# while 和 do-while 循环中的代码重复?
- ssis - SSIS 未能创建任务。无法将任务添加到控制流
- c# - 在这两者之间建立单一的通用服务
- javascript - 无法在 Div 和 Canvas 上写入文本
- floating-point - 如果某个系统上的浮点数存储有一个符号位、一个 3 位指数和一个 4 位有效数:
- javascript - 有没有办法在使用 vanilla JS 的 javascript 生成的表中获取 TD 的值?
- powershell - Powershell Speed:如何加快 ForEach-Object MD5/哈希检查
- javascript - 使用 fetch 后数组值变得未定义