C ++ crazy typedef: what's the point of allowing this syntax to be standard?

Old friend:

typedef int cute_int; //cute : common and familiar syntax. 

This syntax is perfect. No problems.

Now that we can write typedefs as described above, what is the meaning of this syntax:

 int typedef crazy_int; //crazy : uncommon and unfamiliar syntax. 

Just to confuse programmers? Is this syntax even necessary (when in fact we have the previous one)? What do you think, from the point of view of compilers? Do they find it cute or crazy? Or does it not matter at all to compilers?




By the way, this code appeared here: Using the typename keyword with typedef and new

If you are wondering if this is a syntax error, look at the working code here on ideone.

+13
c ++ typedef iso user-defined-types
Dec 12 2018-10-12
source share
4 answers

Question: "Why does he bother you?"

The syntax comes from the grammar of declaration qualifiers in C ++, which is very general and is used for many things in C ++. The order of the declaration qualifiers does not matter. Look at them:

 int virtual f() const, *g(int); int ptr1, *ptr2; int typedef const *name1, name2; 

They are equivalent:

 virtual int f() const; virtual int* g(int); int ptr1; int *ptr2; typedef const int* name1; typedef const int name2; 

If you look at them long enough, you may find that the syntax is actually uniform and logical. Reading C ++ grammar can also help.

+10
Dec 12 2018-10-12
source share

I did not know about this syntax, although my g ++ seemed to accept it ... but from a compiler point of view, it greatly complicates the parsing: when you come across an int token, you don't know, re parsing the type definition (and typedef should happen) , or you are analyzing the definition of a variable / function ...

The only meaning that A typedef B is that you consider typedef binary operator (in the sense of assigning a type as A = B ).

+1
Dec 12 2018-10-12
source share

I am not sure if this is allowed by standard. But, as we know, in C ++ there are many things that admit, but have an illogical syntax. For example, such things 1[a], "hello"[2];// ...

0
Dec 12 2018-10-12
source share



All Articles