Unusual typedef use in C++

C++Typedef

C++ Problem Overview


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

What does this typedef statement mean ?

int typedef foo;

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvivekView Question on Stackoverflow
Solution 1 - C++Sven MarnachView Answer on Stackoverflow
Solution 2 - C++Jonathan GrynspanView Answer on Stackoverflow