52

I came across a new use of the keyword typedef in C++.

What does this typedef statement mean ?

int typedef foo;
vivek
  • 4,951
  • 4
  • 25
  • 33

2 Answers2

43

It's the same as

typedef int foo;

i.e. it defines foo to be the type int. While the grammar allows to swap typedef and int in this case, you usually would not do this because it impairs readability.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 10
    Yes, because `typedef` is a *decl-specifier*, it can go before or after the type, just like `const`. Never noticed that before. – Ben Voigt Sep 20 '11 at 19:39
  • 38
    `#define ALSO_KNOWN_AS typedef /* ;-) */` – Lambdageek Sep 20 '11 at 19:43
  • 1
    Oh. God. Is that strictly `C++` or in `C99`? – ZJR Sep 21 '11 at 00:02
  • 1
    @Lambdageek That's **awesome** (though I won't ever use it, because of the `define`thing, and because it would just confuse the reader... function declaration typedefs are confusing enough...). – paercebal Sep 21 '11 at 08:00
20

typedef is a decl-specifier, so it has the same syntax rules as const or static. It can be moved about like that and will mean the same thing.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • 3
    No, `typedef` and *storage-class-specifier* are both kinds of *decl-specifier*, but `typedef` is not a *storage-class-specifier*. – Ben Voigt Sep 20 '11 at 19:40