Is double-braced scalar initialization allowed by the C++ standard?

Viewed 1768

I have the following code:

int x = {{}};

Is this syntax valid according to the C++ standard? (I'm interested in C++11 and later.)

When using the latest compilers there is no problem, however in some older ones (e.g. GCC 4.8.5) it gives following error:

error: braces around scalar initializer for type 'int'

1 Answers

This is ill-formed. gcc is wrong to accept it and clang seems to allow it as an extension, as it warns about it.

I'm going to quote the latest draft, but it doesn't make a difference. List initialization works as follows as per [dcl.init.list], where T is int in this case:

  • If the initializer list is a designated initializer list, [...] => it's not
  • If T is an aggregate class [...] => it's not
  • If T is a character array [...] => it's not.
  • If T is an aggregate [...] => it's not (only arrays and classes can be aggregates)
  • If the initializer list has no elements [...] => it doesn't
  • If T is a specialization of std::initializer_list [...] => it's not
  • If T is a class type [...] => it's not
  • If T is an enumeration with fixed underlying type [...] => it's not
  • If the initializer list has a single element of type E [...] => a braced initializer list has no type, so no
  • If T is a reference type [...] => it isn't
  • If the initializer list has no elements [...] => it doesn't
  • Otherwise the program is ill-formed
Related