8 ms·
> Maybe that’s the reason people who use the C language love it so much. You can’t do that in any other language. But you can't do it in C. __typeof__ is a GNU
by ogoffart 8y ago
> Maybe that’s the reason people who use the C language love it so much. You can’t do that in any other language.
But you can't do it in C. __typeof__ is a GNU extension.
> Note that wl_list is not a very safe data structure, in the sense that a programmer need to know exactly how to use it.
You can do it in C++ with template, and it would be type safe.
- twtw 8y agotypeof is a GNU extension, but it (or an equivalent) is implemented in every compiler anyone actually uses. You can do it in C, you just can't do it in the part of C the standards committee has decided to standardize. I'm not sure what the value is of defining C as standard C when loads of software has shipped for decades using this pattern.
- comex 8y ago...with the notable exception of MSVC.
- taneq 8y agoAnd there you have it. Purists claim C or C++ can't do X because it's UBH in the standard. Pragmatists know that C and (especially) C++ aren't languages on their own, while "gcc" and "MSVC++" are complete (and very similar) languages.
- adrianN 8y agoAnd careful people keep in mind that only what's defined in the standard can be expected to be true across compiler updates.
- AstralStorm 8y agoNot even that. Compilers had and have bugs.
- adrianN 8y agoIt's not a bug if it's UB. If the compiler behaves differently than the standard says it should you can file a bug.
- tom_ 8y agoThe compiler vendor can treat UB however they like. One option would be for them to specify that it does something in particular... (The C standard explicitly calls this out as a valid way of handling UB.)
- taneq 8y agoGood clarification, you really only know what you're going to get on (language, compiler, compiler version, platform).
- adrianN 8y agoYou also need to know the exact implementation of your compiler's optimizer, otherwise you don't know whether it's safe to change your program. Any change might give the optimizer enough information to unleash the nasal demons in previously cromulent code.
- twtw 8y agoSure, for UB. But extensions like typeof are different from UB. They have documented behavior, it is just documented by GNU instead of the C standards committee.
- ainar-g 8y ago>But you can't do it in C. __typeof__ is a GNU extension. Yes you can. You just have to provide the type every time you use the macro yourself. You can even create a macro that creates helper “methods” for you. With that said, I really dislike how common it is for C programmers to just use whatever their compiler's authors felt like adding and still calling it “portable C”. At this point, if you dislike “plain” C so much, you might as well just switch to a subset of C++ or D's “Better C” mode.
- ogoffart 8y agoYeah, i know you can even do it without providing the type: #define wl_container_of(ptr, sample, member) \ (void*)((char *)(ptr) + ((char*)(sample) - (char*)(&(sample)->member)))