c++ - C++ 在编译时评估 constexpr 模板数组值
问题描述
我在编译时创建了一个打包字符数组,但 c++ 告诉我表达式必须有一个常量值——不能评估不完整的“梯度<256U>”的值。
我正在尝试在我的代码中嵌入颜色渐变。
template<unsigned S>
struct gradient {
constexpr gradient() : arr() {
for (auto i = 0; i < S; i++)
for (auto j = 0; j < S; j++)
arr[i + j * S] = ((i % 256) << 24) | ((j % 256) << 16) | (((i * j) % 256) << 8) | 255;
}
unsigned arr[S * S];
};
constexpr auto g0 = gradient<255>(); // ok
constexpr auto g1 = gradient<256>(); // error
constexpr auto g2 = gradient<1024>(); // error
突然之间,我无法创建具有高于 255 的值的渐变结构。但是为什么呢?
解决方案
推荐阅读
- css - Bootstrap 制作跨度宽度不应超过图像大小
- javascript - Javascript查找字符串中的所有出现
- python - Django Queryset 过滤器性能优化
- javascript - 可拖动文本框的缩放超出预期
- android - 如何导入从 Gmail 保存的联系人?
- java - Spark Java 将数据集转换为不同的类型
- linux - SSH 的问题
- python - AttributeError - 无法访问文件内的函数
- python - python - 如何在不关闭然后打开浏览器的情况下用python中的selenium webdriver模块替换地址?
- flutter - 在 SingleChildScrollView 内的 Column 内 TabBar 内的 ListView