c++ - Is "typedef" in between the type and the alias standard-conformant? -


i stumbled upon code typedef keyword in between type , alias name in

int typedef int; 

it compiles in gcc , clang (live example). not in understanding standardese. question is: standard conformant? can rely on compilers support it?

tl/dr version

yes, int typedef int conforming.

james michener version

c declaration syntax (c 2011 online draft):

6.7 declarations

syntax

1    declaration:
        declaration-specifiers init-declarator-listopt ;
        static_assert-declaration

    declaration-specifiers:
        storage-class-specifier declaration-specifiersopt
        type-specifier declaration-specifiersopt
        type-qualifier declaration-specifiersopt
        function-specifier declaration-specifiersopt
        alignment-specifier declaration-specifiersopt
...

what says in single declaration can have sequence of 1 or more declaration specifiers, each declaration specifier can storage class specifier (auto, extern, static, typedef), type specifier (int, float, char, etc.), type qualifier (const, restrict, volatile, etc.), function specifier (inline), or alignment specifier.

the order in various specifiers appear doesn't matter; static const short int x; may written int static short const x, or int short const static x, etc. matter of practice, people put storage class specifier first, function or alignment specifiers (if necessary), type qualifiers, type specifiers, static const short int x how most people write declaration.

this syntax allows write types long double or long long or unsigned long int, etc.

note while syntax allows arbitrary (and arbitrarily long) sequences of type specifiers, there's semantic rule allows relative few. can't write short short short short x, example, or long long long double y. following sequences allowed:

constraints

2    at least 1 type specifier shall given in declaration specifiers in each declaration, , in specifier-qualifier list in each struct declaration , type name. each list of type specifiers shall 1 of following multisets (delimited commas, when there more 1 multiset per item); type specifiers may occur in order, possibly intermixed other declaration specifiers.
        — void
        — char
        — signed char
        — unsigned char
        — short, signed short, short int, or signed short int
        — unsigned short, or unsigned short int
        — int, signed, or signed int
        — unsigned, or unsigned int
        — long, signed long, long int, or signed long int
        — unsigned long, or unsigned long int
        — long long, signed long long, long long int, or
            signed long long int
        — unsigned long long, or unsigned long long int
        — float
        — double
        — long double
        — _bool
        — float _complex
        — double _complex
        — long double _complex
        — atomic type specifier
        — struct or union specifier
        — enum specifier
        — typedef name

addendum

as keith points out in comment below, future revision of language may limit storage class specifiers beginning of declaration, int typedef int may not legal under future compiler.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -