by _gabe_ · 4y ago · 0 comments
I'm curious how familiar you are with C? In C++ you can do:
struct Foo {
// ...
};
Foo myVar;
But that's not how it works in C. In C this would be: struct Foo {
// ...
};
struct Foo myVar;
Which is why many C developers typedef the struct so that they don't have to prefix struct types with the keyword.> I don't think I can get further into the primer than this... even taking the author at their word that the two snippets are equivalent
Maybe don't judge the author on these things if you're not familiar with how C would work in this case? There's nothing wrong with not understanding a piece of code, but it's generally not a good idea to assume you have understanding of a language like C if you understand C++. People often conflate the two, but there are many quirks of C that C++ doesn't necessarily need to do and vice versa.
Oh, hello. Yes, guilty.
Deprecate this requirement already and make it an option. Gcc --stupid-typedef-required
Edit: or stated in another way, please describe the good reason(s) for having typedef struct {} Foo in C. Do it in terms of justifying the extra typing / extra steps. I'd really like to know. This is extra code and one more thing to test and confirm. Don't reference history or "its just how its always been done" - that to me is an admisson that it needs to go into an option. Also you'll need to explain why its bad that C++ doesn't require this. I'd also like to see as part of this rebuttal a formal statement that C++ got it completely wrong.
So: Why in C do we require the extra steps just so we can get rid of the struct keyword having to be plastered everywhere?
Personally I think the typedef struct pattern is kept purely for historical reasons and is no longer necessary. It obscures the code rather than clarifies it. There are much better reasons for using typedef but getting rid of "struct" prefixes everywhere isn’t one of them.
Its just a code smell. There are likely other bugs hiding around the code. Business logic issues as well. Overall its a story of generally poorly written and ambiguous code. So... Perhaps fix it?
If you can't fix it then a compiler option would do it.
What you're asking for is equivalent to changing the semantics of the language, which is frankly untenable for C.