tags:

views:

133

answers:

5

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 otherwise I'm primarily interested in C++ if that helps.

+16  A: 

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.

James McNellis
This is very useful thanks, I can immediately see the difference now, what is still slightly confusing is the 'two arguments', am I right in clarifing that for `typedef int(*F)(size_t);` there is only one argument to typedef?
Peter McGrattan
@Peter: It's not really an argument at all; it's just the syntax of a declaration. `int(*)(size_t)` is the type of a pointer to a function that returns an `int` and that has a single parameter of type `size_t`. A function to which such a pointer points must take a single argument (of type `size_t`), but that's the only "argument" here.
James McNellis
@Peter: `typedef` isn't a function, it doesn't take arguments. It turns a variable declaration into a type declaration.
GMan
@James: I can see now how to read a type declaration using typedef correctly, this wasn't obvious from several different documentation sources I've read up to now - thank you!
Peter McGrattan
@GMan: Eureka! :)
Peter McGrattan
A: 

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.

linuxuser27
+1  A: 

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.

llasram
+5  A: 

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.

Steve Jessop
+1 for spec reference.
linuxuser27
This is also very helpful although I could still do with clarification on the number of arguments to typedef, is it worth including the correct syntax of typedef here or perhaps a link to elaborate on your first sentence?
Peter McGrattan
@Peter: `typedef` doesn't have any arguments, just like `static` doesn't have arguments, or `const` doesn't have arguments. It doesn't have it's own syntax, either. It's a modifier stuck on the beginning of a declaration, so it's part of the declaration syntax. The declaration syntax and semantics basically occupy the entirety of chapters 7 and 8 of the C++ standard.
Steve Jessop
I think C++ has that distinction because you can put storage class specifiers inside decl specifiers, but not the other way around. E.g. 'typedef const static int foo;' is okay but 'static typedef const foo int;' is not.
Mike DeSimone
@Steve: thank you!
Peter McGrattan
+1 I like this answer!
FredOverflow
+2  A: 

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];
AndreyT