Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does uniform initialization not work in this case? [duplicate]

int main(int argc, char const *argv[])
{
   int x =  4;
   int y = 2;
   const int cell = x/y;
   auto a = std::bitset<20>{cell}; //fails
   auto b = std::bitset<20>(cell); //works
}

Why does std::bitset not allow me to construct with curly braces here, but works with parenthesis construction? If cell was instead a constexpr, both would compile.

The compile error:

test.cpp:21:29: error: non-constant-expression cannot be narrowed from type 'int' to 'unsigned long long' in initializer list [-Wc++11-narrowing]
   auto a = std::bitset<20>{x*y}; //fails
                            ^~~
test.cpp:21:29: note: insert an explicit cast to silence this issue
   auto a = std::bitset<20>{x*y}; //fails
                            ^~~
                            static_cast<unsigned long long>( )
1 error generated.
like image 720
24n8 Avatar asked Jun 01 '26 16:06

24n8


1 Answers

The failing line uses list-initializing syntax:

auto a = std::bitset<20>{cell}; //fails

This syntax is defined in Section 11.6.4 of the C++17 standard. The relevant part:

List-initialization of an object or reference of type T is defined as follows:

...

(3.7) Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (16.3, 16.3.1.7). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.

...

A narrowing conversion is an implicit conversion

...

(7.4) from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression whose value after integral promotions will fit into the target type.

This gives us a better understanding of what is going on:

// Works, no narrowing check, implicit conversion.
std::bitset<20> a(2);
std::bitset<20> b(-1);
std::bitset<20> c(cell); 

// Works, 2 can be converted without narrowing
std::bitset<20> d{2};

// Fails, -1 cannot be converted without narrowing
std::bitset<20> e{-1};

// Fails, compiler does not understand cell can be converted without narrowing
std::bitset<20> f{cell};

In your program, the compiler does not understand that cell is a constant expression. It checks the available constructors for std::bitset and sees it has to convert from int to unsigned long long. It thinks that int could be potentially be negative, therefore we have a narrowing conversion.

We can fix this by making cell a constexpr, which is stronger than const. Whereas const only means that the value should not be changed, constexpr means the value is available at compile time:

  constexpr int x = 4;
  constexpr int y = 2;
  constexpr int cell = x / y;

  auto a = std::bitset<20>{cell}; // works

You could now ask why list-initialization does not allow for narrowing conversion. I cannot fully answer this. My understanding is that implicit narrowing is generally seen as undesirable because it can have unintended consequences, and that it was excluded for this reason.

like image 140
f9c69e9781fa194211448473495534 Avatar answered Jun 03 '26 05:06

f9c69e9781fa194211448473495534



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!