0

This code test.cc

#include <iostream>
int main(){
    int a = 10;
    bool b {a};
    bool c (a);
    std::cout << a
              << " "
              << b 
              << " "
              << std::endl;
    return 0;
}

In clang or gcc

$clang++ -w test.cc
<source>:4:13: error: non-constant-expression cannot be narrowed from type 'int' to 'bool' in initializer list [-Wc++11-narrowing]
    bool b {a};
            ^
<source>:4:13: note: insert an explicit cast to silence this issue
    bool b {a};
            ^
            static_cast<bool>( )
1 error generated.

$g++ -w test.cc ; ./a.out
10 1

Clang just gives an error message on line 4 but missed line 5. While in GCC, it compiles well.

Reproduced in godbolt : https://godbolt.org/z/rzxTSg

I am wondering what is the difference between using "b {a};" and "c (a);" to initialize a value.

I guess I might miss something, could someone help me out? Thanks ~

Haoxin
  • 21
  • 3

0 Answers0