Show HN: Curious Namespace Trick – Limited static polymorphism in C++
github.com
github.com
I know it says it is a trick, but I'm not sure I completely get it.
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/CppC...)
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...
Is there some other alternative that allows 'compile-time static overrides' with relatively clean syntax?
"This technique is an alternative to weak linking,..."
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.
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).