Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using typedef in declaring structs in C++

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

like image 671
Valentin H Avatar asked Nov 21 '25 20:11

Valentin H


2 Answers

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.

like image 115
Marcus Borkenhagen Avatar answered Nov 23 '25 11:11

Marcus Borkenhagen


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.

like image 20
sbi Avatar answered Nov 23 '25 12:11

sbi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!