5 ms·
Show HN: Curious Namespace Trick – Limited static polymorphism in C++
- jaybosamiya 11y agoIs it just me, or is this just an extremely convoluted way of doing things? I know it says it is a trick, but I'm not sure I completely get it.
- klodolph 11y agoI agree... if I see an `#if defined _WIN32` I know what I'm looking at, but this trick would be a complete surprise.
- Noughmad 11y agoIt is. The page starts with talk about namespaces and such, so one thinks this will be about some cool new features of C++11, but instead ends with a global #define. Yes, coding like this is evil and will eat your hamster. For example, trying to have a variable named "platform" will break your code in strange ways. And before you say "but it's C++, it's modern", CppCoreGuidelines explicitly say this is bad and that one should favor #ifdefs. (https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es30-dont-use-macros-for-program-text-manipulation https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...)
- dchichkov 11y agoauthor Yes, "PLATFORM" should be clearly marked with uppercase. Fixed. Thank you. There have to be some kind of "-DPLATFORM=arm" definition, one have to tell to the compiler in some way what hardware we are running on...
- nkurz 11y agoThe title says "C/C++", but the sample code is C++, and I can't find any mention of support for "inline namespace" in C. Are there C compilers that support it as an extension?
- klodolph 11y agoNo, the post says "or C-style code", which is presumably just C++ when you aren't using classes or templates or something like that.
- _kst_ 11y agoI wouldn't call that "C-style code". Namespaces are a C++-specific feature, and inline namespaces were introduced in C++-11. C has never had either feature.
- dang 11y agoOK we took out the "C/" bit above.
- dchichkov 11y agoThank you. Yes - I forgot that there are no namespaces in C. Still, it does works for an approximation of 'C-style code' (no templates, virtual functions, etc).
- TickleSteve 11y agoHow about using weak linking instead...
- vowelless 11y agoFrom the page: "This technique is an alternative to weak linking,..."
- Natsu 11y agoI think they're asking why one might use this instead of weak linking, which is something the article does not explain.
- dchichkov 11y agoauthor here. Unlike weak linking, this method works for functions, templated functions and templates. The design spec can say - only static linking, good inlining during compile-time, no full-program-optimization will be performed. Even if allowed, I'm not sure, if weakly linked functions are being inlined during link-time-optimization step.
- Natsu 11y agoThank you.
- Jun8 11y agohttps://www.reddit.com/comments/bzqjd/metaprogramming_is_the_language_feature_that/?sort=random https://www.reddit.com/comments/bzqjd/metaprogramming_is_the...
- dchichkov 11y agoYes! The problem is, alternative implementations are also meta-programming tricks! Template meta-programming techniques like 'C++ Traits' and 'Curiously recurring template pattern'. Both are being used in the performance-critical places by the way (think Eigen library, etc). Is there some other alternative that allows 'compile-time static overrides' with relatively clean syntax?
- ursus_bonum 11y agoThis is perfect because I was just feeling that C++ wasn't already obfuscated enough.
- dchichkov 11y agoauthor The other alternative, which is currently used in production to solve the same problem is CRTP ( https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern https://en.wikipedia.org/wiki/Curiously_recurring_template_p... ) ... And the question is, which one is a lesser evil.