4 ms·
> Very confused by this. The C code declares an anonymous struct type, then aliases the typename "Foo" to that anonymous struct type. The C3 code seems to decla
by _gabe_ 4y ago
> Very confused by this. The C code declares an anonymous struct type, then aliases the typename "Foo" to that anonymous struct type. The C3 code seems to declares a named struct type "Foo" -- why isn't the C equivalent here just "struct Foo"?
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.
- Cloudef 4y agoI hate when c++ does this. I don't want automatic typedefs, keep the struct so I know what it is. Also causes annoying name conflicts ...
- boardwaalk 4y agoThe number of languages that have separate namespaces for the two things is very small. See all the C descendants that discarded that idea: C++, C#, Java, Rust, Go. You probably ought to just find a decent naming scheme to avoid those conflicts…
- Ontonator 4y agoI can’t speak for the others, but Rust definitely has separate namespaces for them. See https://doc.rust-lang.org/beta/reference/names/namespaces.html https://doc.rust-lang.org/beta/reference/names/namespaces.ht...
- Bytewave81 4y agoNot the type of namespace being referred to. In this case, it's referring to the requirement to refer to all types of a certain category with a keyword, as in `struct Foo`. In Rust, you can refer to a struct named Foo as just `Foo`.
- Ontonator 4y agoI disagree. There may not be a keyword required to refer to `struct`s specifically, but the namespace that is searched when resolving a symbol is determined by the syntax used to refer to it. For example, in C++, you cannot determine whether `a` is a type or a value in the statement `a * b;` without knowing how it is declared, and C also has this ambiguity once `typedef`s are involved. In Rust, there are no such ambiguities (unless there's something I've missed).
- hvdijk 4y agoC++ didn't discard that idea entirely, you can have a struct tag and a function with the same name and you need to be able to do that in order to use stat().
- deleted 4y ago[deleted]
- xigoi 4y agoWhy would you want to expose implementation details?
- lwswl 4y agoc++ just understands that the only purpose you could e using it for is "struct foo". if you have a function "int foo()" as well, then you'll still need the struct tag(which really is necessary because you can do things like call structs like functions)
- SV_BubbleTime 4y ago> Which is why many C developers typedef the struct so that they don't have to prefix struct types with the keyword. Oh, hello. Yes, guilty.
- adamius 4y agoWrapping structs in typedefs like this should be a distant and quaint memory by now. C'mon guys its 2022. Why hasn't some standard gotten rid of this requirement for typedefs around structs? 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.
- Cloudef 4y agohttps://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs https://www.kernel.org/doc/html/v4.10/process/coding-style.h...
- adamius 4y agoIf you can't figure out what vps_t means in your codebase then you have much much bigger issues than typedefs.
- Findecanor 4y agoChanging it would break existing code. In C, types "A" and "struct A" could have been defined as completely different types.
- adamius 4y agoSounds like: We've always done it this way. Often rephrased as: Its not possible to write a script to fix this. We can't have automated tests. There's no way we can find all the places this is likely to be used. There's no way to do this at all because insert-authority-figure's-opinion. Oh no! But really its all historical rather than a real issue. 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.