Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two curiosities with the C++ grammar

In §5.2[expr.post]/1 we have the definition of expression-list

expression-list:
     initializer-list

Why do we need both definitions?

In §8.5[dcl.init]/1 we have:

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

Why do we need the optional , above?

Note that this snippet compiles:

struct A{
    int i;
    float f;
    A(int i, float f) : i(i), f(f) {}
};

int main()
{
    A a = { 1, 2., };
}
like image 415
Belloc Avatar asked Oct 18 '15 16:10

Belloc


People also ask

What are the 10 examples of modals?

There are ten types of modal verbs: can, could, may, might, will, would, shall, should, must, ought to.

What is C in English grammar?

[su_dropcap style=”flat”]T[/su_dropcap]he letter C is the third letter in The English Alphabet. It is also the second Consonant in The English Alphabet. As with every letter in The English Alphabet, The Letter C has two versions in the written form. These two versions are called: The Upper-Case and The Lower-Case.

What is unique about English grammar?

no irregular, or suppletive forms in the comparative and superlative forms in adjective and adverbial systems. uniform stressed syllable intonation. preserved several words or forms exactly as they are reconstructed for the distant proto-language (PIE)

What is grammar and examples?

The definition of grammar is the study of the way words are used to make sentences. An example of grammar is how commas and semicolons are supposed to be used. noun. 35. 7.


2 Answers

The optional trailing comma makes it easy to write code that is extendable without modifying existing lines (nice for source control).

For example, making an array:

int x[] = {
    1,
    2,
    3,
};

If you want to add to it, you just add 4, on a new line, and avoid modifying the line with the 3, at all. It's not necessary, but it's nice to allow it.

like image 62
ShadowRanger Avatar answered Oct 29 '22 23:10

ShadowRanger


The trailing comma is a legacy from C, we can see this justified in the C99 rationale:

K&R allows a trailing comma in an initializer at the end of an initializer-list. The Standard has retained this syntax, since it provides flexibility in adding or deleting members from an initializer list, and simplifies machine generation of such lists.

and this was also the reason cited in comp.lang.c++.moderated: Are comma-separated lists ending in a comma legal?:

yes it is legal. and one reason is to make every line syntactically similar to help automatic tools to deal with large initialization lists

We can find a more detailed background on the differing styles of initialization in the Annotated C++ Reference Manual(ARM), which explains the origins of some of the oddities in the grammar, it says the following about [dcl.init] with emphasis mine:

There are clearly too many notations for initializations, but each seems to serve a particular style of use well. The ={initializer_list,opt} notation was inherited from C and serves well for the initialization of data structures and arrays. [...] The =expression notation also comes from C and is most natural for initializing simple variables, especially of arithmetic or pointer type [...] The {expression-list} notation for initialization came from Simula and seems best when one is creating objects of types that do not for the arithmetic mold.

like image 38
Shafik Yaghmour Avatar answered Oct 29 '22 23:10

Shafik Yaghmour