简体   繁体   English

boost :: integer_mask gcc编译器错误

[英]boost::integer_mask gcc compiler error

I'm unable to compile the following code (using boost 1.61 ). 我无法编译以下代码(使用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;
}

On clang it compiles fine. 在c上它可以很好地编译。
With g++ (version 6.2.1) I get 使用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 )) );
     ^

What am I doing wrong? 我究竟做错了什么?
Is this a gcc or a boost bug? 这是gcc还是boost错误?

Let's analyze the ~(least( 0u )) << Bits expression. 让我们分析~(least( 0u )) << Bits表达式。 Why does this become -1? 为什么变成-1? First, the least type will be the smallest type that can hold the number of bits you want (1). 首先, least类型将是可以容纳所需位数的最小类型(1)。 This will be unsigned char , so the expression now looks like ~(unsigned char( 0u )) << Bits . 这将是unsigned char ,因此表达式现在看起来就像~(unsigned char( 0u )) << Bits The ~ operator will perform integral promotions. ~运算符将执行积分提升。 This results in the unsigned char being promoted to int (signed), because that type is the first in the list of types that an unsigned char can be promoted to and it will hold all the possible values. 这会导致将unsigned char提升为int (有符号),因为该类型是可将无符号字符提升为的类型列表中的第一个类型,它将保存所有可能的值。 The ones' complement of 0, when interpreted as a signed integer in a system that uses a twos complement is -1. 当在使用二进制补码的系统中解释为带符号整数时,0的1的补码为-1。 The shift left of a negative integer is undefined behavior, which is why you get the error you see. 负整数的左移是未定义的行为,这就是为什么看到错误的原因。

sig_bits_fast has this same problem, so it does not provide a solution. sig_bits_fast也有同样的问题,因此没有提供解决方案。

This is a boost bug, since the boost header code relies on undefined behavior (left shift of a signed quantity). 这是一个增强错误,因为增强标题代码依赖于未定义的行为(有符号数的左移)。

I do not think that you do anything wrong. 我认为您没有做错任何事情。 It is clearly compiler bug. 显然是编译器错误。 I compiled your code with online http://cpp.sh/ compiler. 我使用在线http://cpp.sh/编译器编译了您的代码。

I could also check it with VS2015 and boost 1.58. 我也可以使用VS2015进行检查并提升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;
}

Result: 结果:

bitmask <0> = 0
bitmask <1> = 1
bitmask <2> = 3
bitmask <3> = 7
bitmask <4> = 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM