template boy
template boy

Reputation: 10480

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

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.

Upvotes: 10

Views: 310

Answers (1)

Mert Mertce
Mert Mertce

Reputation: 1634

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{};

Upvotes: 2

Related Questions