Synonym value in typedef

The following paragraph is taken from [dcl.typedef]:

As part of its declaration, a typedef name is syntactically equivalent to a keyword and names the type associated with an identifier in the manner described in section 8. Thus, typedef-name is a synonym for another type. The typedef name does not introduce a new type like a class declaration (9.1) or an enum declaration does.

Another excerpt we need from [dcl.type]

Typically, no more than one type specifier is allowed in a complete declaration-seq-specifier of a declaration or in type-seq-specifier or at the end of the type-seq-specifier. The only exceptions to this rule are: ... long can be combined with long ones.

The following code i1

is synonymous with long time.

typedef long i1;
typedef long i1 i2;

      

Thus, I expect the second line to be understood as typedef long long i2

. However MSVC2015RC does not work with

Syntax error C2146 errors: missing ';' before identifier 'i2'

Can anyone point out a part of the standard that makes my expectation unfulfilled?


UPDATE

My point is that as far as I understand the grammar in [dcl.type],

type-specifier:
    trailing-type-specifier
    class-specifier
    enum-specifier
trailing-type-specifier:
    simple-type-specifier
    elaborated-type-specifier
    typename-specifier
    cv-qualifier
type-specifier-seq:
    type-specifier attribute-specifier-seq opt
    type-specifier type-specifier-seq
trailing-type-specifier-seq:
    trailing-type-specifier attribute-specifier-seq opt
    trailing-type-specifier trailing-type-specifier-seq

      

Description-specifier-seq allows a sequence of type specifiers if they satisfy combination rules. It seems to me that the type is not the same as the type specifier, even if the type is specified by the type specifier; -)

+3


source to share


3 answers


Okay, I'll answer.

First, looking at this:

typedef-name is syntactically equivalent to keyword

This means that the typedef names follow the syntax of the keywords. This does not mean that the typedef name is equivalent to any particular keyword. It looks like a new unique keyword.

Then we have <



Thus, typedef-name is a synonym for another type.

So, considering typedef long i1;

what is this "other type"? This long int

, not only long

.

Also, what is "type"? At least the type-specifier is not a type. The type long

specifier represents the type "long int" (see Table 10 of n3337 or Table 9 of n4296).

I'll copy my comment here:

i1

is a synonym for type long int

. It is not synonymous with a keyword long

. Otherwise, you can also say i1 double

and receive long double

.

Although perhaps I should have said: is i1

not a synonym for a type specifier long

, but is a synonym for a type long int

.

+4


source


AFAIK, there is no syntax rule that says what long

T

is a valid type when it T

is a valid type. (But there is a certain rule for classifiers like volatile

or const

)

In other words, long long

it should be seen as a "verbose keyword" (but the C and C ++ standardization committees are very reluctant to introduce new keywords).



Hence, I do not expect the following to be valid

 // probably invalid
 typedef int fooT;
 unsigned fooT barv;

      

+3


source


Paragraph 2 [dcl.type] has rules long

, short

, signed

, unsigned

, const

and volatile

.

As a general rule, no more than one type of specification is allowed in a full declaration-declaration or in a type-segment-specifier or trailing-type-specifier. The only exceptions to this rule are as follows:

  • const can be combined with any type other than itself.

  • volatile can be combined with any type other than itself.

  • signed or unsigned can be combined with char, long, short, or int.

  • short or long can be concatenated with int.

  • long can be combined with double.

  • long can be combined with long ones.

Thus,

typedef long i1;
typedef const i1 i2;

      

is valid since it const

can be combined with any type specifier, and

typedef long i1;
typedef long i1 i2;

      

no, because it i1

is a name for long

, but it is not a specifier long

.

i1

in your example may syntactically be equivalent to the keyword, but it is not among the allowed specifiers that can be combined with long

. This is a different keyword and therefore the rule for combination with long

does not apply to it.

+1


source







All Articles