由于非类型模板参数而具有零大小数组的类模板:如何防止警告
Class-template with zero-size array due to non-type template parameter: how to prevent warning?
我使用类似于以下最小示例的代码:
#include <stdint.h>
constexpr uint8_t size = 0; // sort of global config option
template<typename T = char, uint8_t L = size>
struct A {
T f() {
if constexpr(L > 0) {
return data[0];
}
return T{0};
}
int x = 0;
T data[L];
};
int main() {
A x;
x.f();
}
现在我将编译器 (g++( 设置更改为 -pedantic,并收到以下警告:
ISO C++ forbids zero-size array [-Wpedantic]
这绝对没问题,但我想知道如何防止此消息?
您可以将A
结构专门用于L == 0
以下情况
template <typename T>
struct A<T, 0>
{
T f() { return {0}; }
};
相关文章:
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- G++ 发出警告,要求删除一个代码的数组,但不删除另一个代码的数组
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 如何使用GSL :: SPAN来修复任何数组以降低指示警告
- C4838 警告,包含常量字符* 数组的数组初始化
- 如何消除"仅使用常量表达式索引到数组"警告?
- 警告:数组"字母表"由括号字符串文本初始化
- PTXAS 在结构数组上使用 thrust::sort 时"double is not supported"警告
- 关于数组初始化的警告
- 严格的混叠警告,创建对无符号字符数组 + 偏移量的uint32_t引用
- 警告:在 C++ 中比较数组和字符串
- 警告:数组下标高于数组边界 [-Warray-bounds]
- #数组初始值设定项内部忽略了pragma警告禁用
- 警告在数组索引中使用的对int-0的引用
- constexpr 返回数组,GCC 警告
- 在无符号字符数组和有符号字符数组上使用strlen时发出警告
- 对静态数组的初始化列表大小错误发出警告
- g++在虚拟析构函数中给出了删除数组的警告消息,这意味着什么
- 对混合数组和多态的编译器警告
- 使用我的数组类时的奇怪警告