Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

syntax

c++11

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.

like image 220
user4309946 Avatar asked Dec 01 '14 00:12

user4309946


1 Answers

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.

like image 198
harmic Avatar answered Oct 02 '22 18:10

harmic