How does initializing an integer as "a = {1,}" compile?

c++, c++11, syntax

Solution

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.

Problem

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.

Original source

Related problems