开发者

Fundamental typedef operand syntax

Given:

typedef type-declaration synonym;

I can see how:

typedef long unsigned int size_t;

declares size_t as a synonym for long unsigned int, however I (know it does but) can't see exactly how:

typedef int (*F)(size_t, size_t);

declares F as a synonym for pointer to function (size_t, size_t) returning int

typedef's two operands (type-declaration, synonym) in the first example are long unsigned int and size_t.

What are the two arguments to typedef in the declaration of F or are there perhaps overloaded versions of typedef?

If there is a relevant distinction between C and C++ please elaborate otherwis开发者_JAVA技巧e I'm primarily interested in C++ if that helps.


Type declarations using typedef are the same as corresponding variable declarations, just with typedef prepended. So,

        int x; // declares a variable named 'x' of type 'int'
typedef int x; // declares a type named 'x' that is 'int'

It's exactly the same with function pointer types:

        int(*F)(size_t); // declares a variable named F of type 'int(*)(size_t)'
typedef int(*F)(size_t); // declares a type named 'F' that is 'int(*)(size_t)'

It's not a "special case;" that's just what a function pointer type looks like.


That's not the formal syntax of a typedef, it's just one of the patterns which it can take. In the C standard 6.7.1, typedef is syntactically defined as a storage-class specifier (like extern or static). It modifies a declaration, so that the declaration declares a type alias instead of an object.

typedef isn't either a function or an operator, so notions of "argument", "operand" or "overloading" don't apply to it. It just tells the compiler what kind of declaration you're making.

In C++, typedef is syntactically defined as a decl-specifier, it is not a storage-class-specifier. storage-class-specifiers are also decl-specifiers, and so is friend. I don't think this makes any practical difference, it's another way of saying the same thing C says, but it's 7.1 of the C++ standard if you want to have a look for yourself. I confess it's baffling me for the time being.


Your initial assumption about typedef syntax having the

typedef type-declaration synonym;

structure is absolutely incorrect. Typedef syntax does not have that structure and never had.

Typedef syntax is that of an ordinary declaration, just like any other declaration in C language. Keyword typedef is simply a declaration specifier that indicates that the name declared is a typedef name, and not a variable, function designator or something else.

You can use multiple declarators in the same typedef declaration, for example

typedef int TInt, *TIntPtr, (*TIntFuncPtr)(void), TIntArr10[10];


Instead of thinking of typedef as an operation which takes two parameters (the type and the synonym) think of it as a type qualifier. To declare a variable named F which was a function pointer accepting two size_t parameters and returning int you would have just:

int (*F)(size_t, size_t);

Add the type qualifier typedef and instead of declaring a variable, you've declared a type alias instead.


typedef is using the declaration syntax.

The function pointer typedef is the same as you would use to declare a function pointer. Except in that case you are declaring a type, not variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜