c++ - Couln't someone explain the grammar of typedef once and for all? -
i know how declare aliases simple types, class types, primitive types and, say, pointers functions returning value of types. actually:
typedef int t; //t := int typedef int* t; // t := int* typedef int (*t)() //t := int (*)(). ok, it's bit unclear me. //seems little bit confused typedef int (*t[])() // t := array of int(*)(). totally confused. hell going on?
i can't understand how compiler should parse such typedef declarations. maybe can explain on simple example cited? know, c++
introduced alias decalrtion follows:
using t = int*;
it more readable, i'm interested in typedef decalration.
the grammar of typedef
same of variable declaration; difference name being declared becomes alias type, rather object, reference or function.
note typedef
part of decl-specifier-seq of declaration; full declaration consists of 3 parts: attribute-specifier-seq (new c++11), decl-specifier-seq, , init-declarator-list, in order. parts may in principle empty, types of declarations; in case of typedef
, example, attribute-specifier-seq may empty. understand declaration, have first break down 3 parts: attribute-specifier-seq easy: within [[...]]
, won't see often, since new, , special uses. we'll ignore now. decl-specifier-seq sequence of keywords or symbols name type (although there special cases after keywords, struct
or enum
); collect of symbols until encounter isn't keyword or type. typedef
included. order here isn't important, so:
int typedef const ci;
would legal, although not typical. if keyword typedef
present, declaration typedef
(which means other keywords, extern
or static
, aren't allowed). decl-specifier gives final type in english expression of type.
everything follows part of init-declarator-seq comma separated list of init-declarator. typedef
requires @ least 1 init-declarator, , in fact, doesn't allow init part, declarator (but there can in fact several, although microsoft ones know go in bit of obfuscation). each declarator expression, operators on right (()
, []
) having precedence on operators on left (*
, &
), , parentheses being used modify precedence. if have (&ra)[10]
, ra
reference array[10] of... what:w ever type specified decl-specified. or precedence not given parentheses: *ra[10]
array[10] of pointers to...
Comments
Post a Comment