Hacker News new | past | comments | ask | show | jobs | submit login

C++ has two types of polymorphism;

- Templates (compile time), which are generic bits of code that are monomorphized over every combination of template parameters that they're used with.

- Virtualization (runtime), classic OO style polymorphism with a VTable (I think this similar to Rust's dyn trait?).




I think they meant to say that neither templates (nor virtual classes) are quite up to the same level of polymorphism that Rust has. Ie Rust type checkes at definition, as opposed to copy-paste and hope for the best (wrgt templates) when trying to compile the code. In other words Cpp templates are closer to Rusts declarative macros than to Rust generics. Hopefully concepts make this better in Cpp.


Unfortunately, the concepts we got in C++20 still do not allow for type checking definitions and there isn't a realistic path to get there.

I still think that C++ templates are different than rust macros as they are not a separate pass from type checking.


As always in C++ world, there is a workaround with static analysis, which could give errors when template code reaches out to capabilities that aren't part of the concept definition.

Either that, or switch to Circle I guess.


Does such a static analysis exist? I think the most realistic solution is to write an archetype class for each concept and instantiate each template with its relevant archetypes.

Now can we automate writing archetypes from concepts (and viceversa)? Maybe in C++64 when we finally get static reflection.


Not yet, hence the conditional form.

I really hate how C++/WinRT used the "C++ reflection is around the corner" excuse to kill C++/CX and downgrade the COM development experience back to pre-.NET days.

Apparently it isn't around the corner.

However with type traits and a bit of constexpr if (requires {....}) it is possible to have a kind of poor man's compile time reflection.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: