17

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.

Peter McG
  • 18,857
  • 8
  • 45
  • 53
  • Just to clarify source of initial given syntax is from here: http://msdn.microsoft.com/en-us/library/05w82thz%28v=VS.100%29.aspx – Peter McG Sep 23 '10 at 23:05
  • don't believe everything Microsoft tells you ;-) – Steve Jessop Sep 23 '10 at 23:10
  • Function pointer declarations are some of the weirdest syntax in C. The only thing I've seen that was weirder was pointer-to-member syntax in C++... And that's because it resembles C function pointer syntax. – Mike DeSimone Sep 23 '10 at 23:15
  • @Steve: Thanks :) I wouldn't dream of it: suspicion is usually healthy in my experience which actually led to this question in this instance – Peter McG Sep 23 '10 at 23:16

5 Answers5

32

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
  • 348,265
  • 75
  • 913
  • 977
  • 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 McG Sep 23 '10 at 23:00
  • 1
    @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 Sep 23 '10 at 23:01
  • 3
    @Peter: `typedef` isn't a function, it doesn't take arguments. It turns a variable declaration into a type declaration. – GManNickG Sep 23 '10 at 23:10
  • 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 McG Sep 23 '10 at 23:11
7

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
  • 273,490
  • 39
  • 460
  • 699
  • 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 McG Sep 23 '10 at 23:00
  • 2
    @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 Sep 23 '10 at 23:03
  • 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 Sep 23 '10 at 23:21
2

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];
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

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
  • 4,417
  • 28
  • 28
0

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
  • 7,183
  • 1
  • 26
  • 22