Boost::integer_mask GCC编译错误
boost::integer_mask gcc compiler error
我无法编译以下代码(使用boost 1.61)。
#include <boost/integer/integer_mask.hpp>
#include <iostream>
int main() {
uint a = boost::low_bits_mask_t<1>::sig_bits;
std::cout << "bitmask " << a << std::endl;
}
在clang上它可以很好地编译。
使用g++(版本6.2.1),我得到
file included from /usr/include/boost/config.hpp:61:0,
from /usr/include/boost/integer_fwd.hpp:15,
from /usr/include/boost/integer/integer_mask.hpp:13,
from boostbug.cpp:1:
/usr/include/boost/integer/integer_mask.hpp: In instantiation of ‘const least boost::low_bits_mask_t<1ul>::sig_bits’:
boostbug.cpp:7:42: required from here
/usr/include/boost/integer/integer_mask.hpp:66:5: error: left operand of shift expression ‘(-1 << 1ul)’ is negative [-fpermissive]
BOOST_STATIC_CONSTANT( least, sig_bits = (~( ~(least( 0u )) << Bits )) );
^
我做错了什么?
这是一个gcc bug还是一个boost bug?
分析~(least( 0u )) << Bits
表达式。为什么这个变成了-1?首先,least
类型将是可以容纳您想要的位数(1)的最小类型。这将是unsigned char
,因此表达式现在看起来像~(unsigned char( 0u )) << Bits
。~
运算符将执行积分提升。这将导致unsigned char
被提升为int
(有符号),因为该类型是unsigned char可以提升到的类型列表中的第一个类型,并且它将保存所有可能的值。1的补码为0,在使用2补码的系统中被解释为有符号整数时为-1。负整数的左移是一种未定义的行为,这就是为什么你会看到这样的错误。
sig_bits_fast
也有同样的问题,所以没有提供解决方案。
这是一个boost错误,因为boost头代码依赖于未定义的行为(有符号量的左移)。
我不认为你做错了什么。这显然是编译器错误。我用在线http://cpp.sh/编译器编译了你的代码。
我也可以在VS2015和boost 1.58中检查它。
#include <boost/integer/integer_mask.hpp>
#include <iostream>
int main()
{
uint a0 = boost::low_bits_mask_t<0>::sig_bits;
std::cout << "bitmask <0> = " << a0 << std::endl;
uint a = boost::low_bits_mask_t<1>::sig_bits;
std::cout << "bitmask <1> = " << a << std::endl;
uint b = boost::low_bits_mask_t<2>::sig_bits;
std::cout << "bitmask <2> = " << b << std::endl;
uint c = boost::low_bits_mask_t<3>::sig_bits;
std::cout << "bitmask <3> = " << c << std::endl;
uint d = boost::low_bits_mask_t<4>::sig_bits;
std::cout << "bitmask <4> = " << d << std::endl;
}
结果:bitmask <0> = 0
bitmask <1> = 1
bitmask <2> = 3
bitmask <3> = 7
bitmask <4> = 15
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?