6 ms·
Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” mem
by sesuximo 5y ago
Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).
- pjmlp 5y agoIn C++ we have namespaces for 30 years now, no need for such tricks.
- comex 5y agoC++ namespaces are unrelated to this. They don’t accomplish the same thing.
- pjmlp 5y agoThe goal of inline namespaces is exactly to allow for migrating libraries across versions.
- comex 5y agoThat's nice, but the blog post doesn't say anything about migrating libraries across versions? Looking at the comment thread, I see tialaramex's sibling comment suggested the blog post was about migration, but it's not. I suppose migration is another possible use case for the union trick, and for that case C++ inline namespaces can be used as part of an implementation that achieves a broadly similar goal, but in a completely different way. As tialaramex notes, with inline namespaces you still end up with two different types.
- tialaramex 5y agoHmm. How do C++ namespaces help with the structure naming problem in this example? They seem completely orthogonal. C++ namespaces are a way to avoid library A's symbol "cow" clashing with library B's symbol "cow" without everything being named library_a_cow and library_b_cow all over the place which is annoying. I agree C would be nicer with such a namespace feature. However this technique is about what happens when you realise your structure members x and y should be inside a sub-structure position, and you want both: d = calculate_distance(s.x, s.y); // Old code and d = calculate_distance(s.position.x, s.position.y); // New ... to work while you transition to this naming.
- pjmlp 5y agoYou can use inline namespaces for versioning symbols. https://www.foonathan.net/2018/11/inline-namespaces/ https://www.foonathan.net/2018/11/inline-namespaces/
- tialaramex 5y agoFirst of all, C++ 11 may feel like thirty years ago, and certainly some of its proponents look thirty years older than they did at the time, but it was only ten years ago. C++ namespaces date to standardisation work (so after the 1985 C++ but before the 1995 standard C++) but they don't get this job done. Inline namespaces are a newer feature. Secondly this technique does something different. The C hack doesn't touch the old code. But this "inline namespace" trick means old code has to explicitly opt into this backward compatibility fix or else it might blow up. Lastly, I didn't try this, but presumably you did. Are the two separately namespaces classes the "same thing" as far as type checking is concerned? A vital feature of this union trick is that it's just one structure, it type checks as the same structure because it is the same structure. At a glance, I think the C++ solution results in two types with similar names, so that would fail type checking.
- pjmlp 5y agoAh, another of those threads, ok lets set the years straight. Yes, inline namespaces were only introduced in C++11, about 10 years ago, now lets dive into article. "Learning that you can use unions in C for grouping things into namespaces" Grouping into namespaces, so when did C++ get said feature? ANSI/ISO C++89 released to the world in September 1998, which makes around 23 years, or 24 years if we consider the release of C++ compilers already supporting it the year before, like Borland C++. This C hack definitly does touch old code, as it requires the code to be written to take advantage of the technique and is also touched again, when changes to the structs are required. And naturally recompilation. With inline namespaces, assumign recompilation you can naturally also change which set of identifiers and type aliases are visibile by default.
- 5y ago
- ferdek 5y agoIn other words: please always be wary of differences in C and C++, for instance type punning [0]. [0] https://stackoverflow.com/a/25672839 https://stackoverflow.com/a/25672839
- pjmlp 5y agoTriggering UB is a compiler error in constexpr code. https://shafik.github.io/c++/undefined%20behavior/2019/05/11/explporing_undefined_behavior_using_constexpr.html https://shafik.github.io/c++/undefined%20behavior/2019/05/11...
- sesuximo 5y agoTrue, you’ll hopefully get a compiler error.
- midjji 5y agoConstexpr unions is the sane/safe way to use them. Its great, because accessing a member which isnt the last one written, constexpr will explicitly prevent it compile time. Whereas all other examples here are explicitly undefined behaviour!