Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is the `synonym` in a `typedef` mandatory?

I encountered this in a code review:

typedef struct C { int i; };

It compiles.

Apart from it being C-style, where structs are in a separate 'namespace', and need to be typedeffed in order to use later on, I found it weird that there is nothing defined with this typedef...

So: doesn't typedef require a type argument and an alias argument?

like image 646
xtofl Avatar asked Jan 14 '11 12:01

xtofl


3 Answers

A quick glance over n3225 doesn't show any requirement that a name is required to be present. The text that comes the most near just says

In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaborated- type-specifier with a class-key (9.1), or an enum-specifier.

So your code seems to be valid, but I think it smells bad.

like image 102
Johannes Schaub - litb Avatar answered Sep 28 '22 16:09

Johannes Schaub - litb


Yes, typedef requires another argument. That typedef won't do anything.

Anyway it's not an error, just a deprecate usage of typedef.


C standard (n1256 in 6.7.7) talks about typedef. It doesn't say this usage is deprecated, but compilers report it is, since such statement has no effect.

like image 31
peoro Avatar answered Sep 28 '22 17:09

peoro


Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !

Your Comeau C/C++ test results are as follows:


Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing.  All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 1: warning: declaration requires a typedef name
  typedef struct C { int i; };
                             ^
like image 30
Maxim Egorushkin Avatar answered Sep 28 '22 17:09

Maxim Egorushkin