I would like a standard reference why the following code triggers a compliance warning in C (tested with gcc -pedantic; “typedef redefinition”), but is fine in C++ (g++ -pedantic):
typedef struct Foo Foo;
typedef struct Foo Foo;
int main() { return 0; }
Why can I not define a typedef repeatedly in C?
(This has practical implications for the header structuring of a C project.)
Why does this compile in C++?
Because the C++ Standard explicitly says so.
Reference:
C++03 Standard 7.1.3 typedef specifier
§7.1.3.2:
Why does this fail to compile in C?
typedefnames have no linkage and C99 standard disallows identifiers with no linkage specification to have more than one declaration with the same scope and in the same name space.Reference:
C99 Standard: §6.2.2 Linkages of identifiers
§6.2.2/6 states:
Further §6.7/3 states: