Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using curly braces to value-initialize temporary as initializer to static data member causes error

Tags:

c++

gcc

c++11

This very simple code code gives an error in GCC 6.0:

template<class T>
struct S {
    // error: cannot convert 'T' to 'const int' in initialization
    static const int b = T{};
};

int main() {
}

Strangely, if I use regular braces instead (T()) then the code compiles. Is this a bug? The code compiles fine in clang.

like image 471
template boy Avatar asked Jun 06 '15 00:06

template boy


1 Answers

The reason that T() works is because the compiler interprets it as a function declaration which takes no argument. The compiling would be done with just an explicit casting:

static const int b = (const int) T{};
like image 75
Mert Mertce Avatar answered Oct 14 '22 13:10

Mert Mertce