3

I personally don't like old C-style of struct declarations like that:

typedef struct {} NewType;

ctags makes ugly anon types of this and make the debugging difficult. Is there any reason in C++ code using typedef struct instead of simply struct, except the code is used in both C and C++?

Regards, Valentin

Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • http://stackoverflow.com/questions/385023/why-do-you-use-typedef-when-declaring-an-enum-in-c – Pratik Dec 12 '10 at 10:00

3 Answers3

3

That's a C-ism. In C, structs and enums effectively have their one namespaces, so for

struct NewType {};
enum SomeEnum {};

you'd have to write struct NewType and enum SomeEnum to refer to.

In C++ this isn't necessary. So, Unless you write a header which needs to be parsed by a C compiler, too, you shouldn't use this.

sbi
  • 219,715
  • 46
  • 258
  • 445
3

One major drawback of those

typedef struct tagSomethingSomething SomethingSomething;

is that, forward-declarations are not possible with the commonly used typedef'fed name.

Yes, it's a C-ism (kudos sbi) and there are c++-codebases, where it - unfortunately - (still) is common.

Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
  • It is a good reason, I'll check that! To be honest, I am looking currently for reasons to persuade my collegues not tu use typedef in this case. But unfortunatelly I couldn't find any reasons except annoying anon-types in debugger (they use printf debugging) and old C-style (they are proud of it). – Valentin H Dec 12 '10 at 19:15
  • Valentin: good luck in your conquest :) I too still have coworkers that - in a c++ codebase - use tag names and typedefs... There is really no argument **for** typedefs in c++, one should not have to persuade others. Cheers – Marcus Borkenhagen Dec 12 '10 at 19:39
  • 2
    @Valentin: One argument would be: How do you write a constructor for such a struct? Forward declarations, already mentioned by Fritschy, are another one. – sbi Dec 14 '10 at 11:02
  • 1
    Or an explicit dtor call: `Something *mySomthing; /* ... */; mySomething->~tagSomething();` - That **is** smelly. – Marcus Borkenhagen Dec 14 '10 at 11:06
-1

@sbi: I think no: could be wrong, but .. in C++ it isn't an anonymous struct, there's a special rule called the "struct hack" which makes an ordinary struct tagged with the typedef name, so the typedef name becomes the class name as well. This is required because functions with external linkage require arguments of class type to have external linkage too, and that implies having an external name. Without this hack, only extern "C" functions could manipulate the struct.

The only place you could use an anonymous struct would be inside another structure:

struct X { struct {int a; } b; } x;
x.b.a; // OK

struct X has external linkage even though the type of b is anonymous. Anonymous structs could be used on the stack or in static storage but unlike anonymous unions, that would provide no advantage over separate variables. I'm not even sure C++ allows anonymous structs.

Yttrill
  • 4,725
  • 1
  • 20
  • 29
  • -1. Sorry, I couldn't get the point how this relies to my question. – Valentin H Dec 12 '10 at 19:11
  • The point is your original text stated **incorrectly** that C style typedef'ed structs without tags were anonymous structs. This is not correct in C++, they're not anonymous structs, the typedef name is taken as a tag name and class name and the resulting type even has external linkage. So actually you should use the non-typedef form, since even you got confused about the semantics when a typedef was used, expecting it to be merely an alias for an anonymous struct (which would not have external linkage). – Yttrill Dec 29 '10 at 19:49