禁用杂散"warning: left shift count >= width of type"
disable spurious "warning: left shift count >= width of type"
在使用
gcc 编译一些模板代码时,我收到一个虚假的"警告:左移计数>= 类型宽度":
template <int N> class bitval {
unsigned long val;
#pragma GCC diagnostic ignored "-Wall"
const bitval &check() {
if (N < sizeof(unsigned long) * CHAR_BIT && val >= (1UL << N)) {
std::clog << val << " out of range for " << N << " bits in " << std::endl;
val &= (1UL << N) - 1; }
return *this; }
#pragma GCC diagnostic pop
};
问题是当它被实例化为 N == 64 时,它会发出警告。
现在警告完全是虚假的,因为代码检查 N 是否足够大,不需要检查/掩码,所以当 N 为 64 时,偏移永远不会发生。 但海湾合作委员会还是警告说。 所以我试图禁用警告,但我无法弄清楚关闭它所需的 #pragma 魔法......
标记调度救援:
const bitval& check() {
constexpr bool enough_bits = (N < sizeof(unsigned long) * CHAR_BIT);
using enough_bits_t = std::integral_constant<bool, enough_bits>;
return check( enough_bits_t{} );
}
const bitval& check(std::false_type /*enough bits*/) {
return *this;
}
const bitval& check(std::true_type /*enough bits*/) {
if (val >= (1UL << N)) {
std::clog << val << " out of range for " << N << " bits in n";
val &= (1UL << N) - 1;
}
return *this;
}
决策(是否有足够的位)是一个编译时常量,所以我将其计算为一个。
然后,我根据该决定构建一个类型(std::true_type
或std::false_type
),并调度到两个不同的重载之一。
因此,N
太大而无法移动那么远的代码永远不会实例化,因此不会发出警告。 如果决策中存在逻辑错误,并且代码被实例化,我会收到编译器警告而不是静音(如果您设法禁用警告,并且您遇到逻辑错误,您将获得静默 UB)。
相关文章:
- EASTL矢量<向量<int>>连续的
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- C++ duration_cast<>(time_point_end - tine_point_start)。count() 溢出
- 为什么 std::count 比 MSVC 编译器的普通循环慢,但与 GCC 相等?
- std::chrono::duration::count函数的实际结果类型是什么
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 在给定的代码中,有人可以解释一下(int i = 0; i<len; i++)count[str[i]]++的代码片段;
- Count the digits
- arduino count++ 是否有限制以及如何修复它?
- "cout<<count<<endl;"没有打印任何内容
- 如何使用 c++ 中的 int* count(const string&s)计算字符串中每个数字的出现次数?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 循环遍历向量<string>并保持每个元素"count"时出现问题
- 此陈述基本上是在count [str [j]] 中的意思
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 为什么 std::count 和 std::find 没有针对使用 memchr 进行优化?
- 快速'group by/count' std::vector<std::u16string> 变成 std::map<u16string, int>
- 为什么 MSVC 在实现 std::bitset::count 时不使用 __popcnt?