生成 constexpr 字符串表,不能产生常量表达式
Generate constexpr string table, cannot result in a constant expression
灵感来自 https://stackoverflow.com/a/37413361/1734357 我希望制作一个固定且已知大小的字符串颜色查找表,所以我不需要模板化它,但字符串不是 constexpr
怎么做呢?
struct Colors
{
constexpr Colors() : colors()
{
for (size_t i = 0; i < 256; i++)
colors[i] = "0;" + to_string(i) + ";255";
for (size_t i = 0; i < 256; i++)
colors[256 + i] = "0;255;" + to_string(255 - i);
for (size_t i = 0; i < 256; i++)
colors[2 * 256 + i] = to_string(i) + ";255;0";
for (size_t i = 0; i < 256; i++)
colors[3 * 256 + i] = "255;" + to_string(255 - i) + ";0";
}
string colors[4*256];
};
老实说,我会在 Python 脚本中执行此操作,生成一个C++源文件(由数组定义组成(作为预构建步骤。
这样,您就可以在编译时将信息烘焙到应用程序中,并且信息的来源仍然是易于修改的代码。
constexpr
很好,但有时您只想自动生成一些代码。
问题是to_string返回 std::string,而 std::string 不是 constexpr 可构造的。
这个问题可以通过使用 Sprout constexpr 库来解决。尽管您可能会对编译时间感到失望。
#include <iostream>
#include <sprout/string.hpp>
#include <sprout/array.hpp>
#include <sprout/algorithm/transform.hpp>
#include <sprout/numeric/iota.hpp>
struct colorConv {
typedef sprout::string<20> result_type;
constexpr result_type
operator()(int n) const {
return n / 256 == 0 ? "0;" + sprout::to_string(255 - n) + ";255"
: n / 256 == 1 ? "0;255;" + sprout::to_string(2 * 255 + 1 - n)
: n / 256 == 2 ? sprout::to_string(3 * 255 + 2 - n ) + ";255;0;"
: "255;" + sprout::to_string(4 * 255 + 3 - n) + ";0";
}
};
struct Colors {
private:
typedef colorConv::result_type string;
constexpr static auto source = sprout::iota<sprout::array<int, 256*4> >(0);
public:
constexpr static auto colors = sprout::transform<sprout::array<string, 256*4> >(
sprout::begin(source),
sprout::end(source),
colorConv()
);
};
int main() {
auto& a = Colors::colors;
for (auto&& str : Colors::colors) {
std::cout << str << std::endl;
}
return 0;
}
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 使用自动推导的 lambda 参数作为常量表达式
- 生成提升::hana::set 的常量表达式问题
- 为什么不能用常量表达式声明数组?
- 不是 lambda 函数中的常量表达式
- 函数调用在常量表达式中必须具有常量值
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 生成 constexpr 字符串表,不能产生常量表达式
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何在满足常量表达式的同时将整数传递给指针,传递给 std::array<double、integer>?
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:'new'不能出现在常量表达式中
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- C++ 使用变量而不是常量表达式初始化数组
- 使用函数参数作为常量表达式的一部分 - gcc vs clang
- 片段着色器中的"错误:在 GLSL 1.30 及更高版本中禁止使用非常量表达式索引的采样器数组"