Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 443
Igor Avatar asked Aug 29 '18 10:08

Igor


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
like image 61
Rakete1111 Avatar answered Oct 19 '22 14:10

Rakete1111