如何在gcc或clang中启用从int到int64_t的转换警告
How to enable warnings on conversion from int to int64_t in gcc or clang
是否可以在从int到int64_t的强制转换中启用g++或clang中的警告?示例:
int n;
cin >> n;
int64_t power = (1 << n);
我希望编译器在第三行告诉我这个转换。
您可以在这些行上构建一些东西:
struct my_int64
{
template<class Y> my_int64(const Y&)
{
static_assert(false, "can't do this");
}
template<> my_int64(const long long&) = default;
/*ToDo - you need to hold the data member here, and
supply necessary conversion operators*/
};
然后
int n = 3;
my_int64 power = (1LL << n);
编译,但
my_int64 power = (1 << n);
不会。从这个意义上说,这是一个很好的起点。你可以破解预处理器,用它来代替int64_t
。
如果你想要一个警告而不是错误,你可以用代替static_assert
my_int64 x{}; Y y = x;
和希望编译器发出缩小转换的警告,并相信它能优化这两个语句,因为它们共同是no-op。
相关文章:
- 警告处理为错误这里有什么问题
- 从不同线程使用int64的不同字节安全吗
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 使用typeid警告未使用的变量
- 示例C++项目编译中的警告
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 如何修复编译器警告 C6386 和 C6385?
- 返回语句后的代码,没有警告
- 获取隐式转换溢出从无符号到已签名的警告
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 在未链接的部分上生成警告
- 警告 C4552:">>":未使用表达式的结果
- 禁止显示由于常量为零而比较始终为假的警告
- C++ 警告:将新创建的 gsl::owner<> 分配给非所有者