15

I found this weird syntax:

int a = {1,};

And it works in all the compilers I've tried. How does it compile?

EDIT: I thought that scalar initializers can only have one element in it, spawning my question. Sorry for all the trouble.

user4309946
  • 151
  • 4
  • 3
    Please do everyone a favor, and don't let the author of that line get away unpunished. Call him/her to the water cooler, roll up a newspaper, whack him/her and say "No! Bad! Bad programmer!" – DanielKO Dec 01 '14 at 04:11
  • @DanielKO Of what line? – user4309946 Dec 01 '14 at 04:18
  • 1
    The line you posted, `int a = {1,};`, you said you found it. It's a tongue-in-cheek way to say please don't ever write code like this, unless you are creating a submission for the IOCCC. – DanielKO Dec 01 '14 at 04:21

1 Answers1

15

As stated by Matt McNab in comments, the syntax of a braced initialized list is the same regardless of whether you are using it to initialize a scalar or anything else.

C++11 §5.17 states

A braced-init-list may appear on the right-hand side of

  • an assignment to a scalar, in which case the initializer list shall have at most a single element.

The definition of braced-init-list is (from §8.5):

braced-init-list:
  { initializer-list ,opt }
  { }

where the 'opt' means that the trailing comma is optional.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
harmic
  • 28,606
  • 5
  • 67
  • 91