Compile time "polymorphism"

Published December 20, 2016
Advertisement

So here is something I just discovered I can do in C++ that I didn't know about.


template class wrapper{public: wrapper(const T &t){ } int length() const { return -1; }};template<> class wrapper{public: wrapper(const StringEntity &e) : e(e) { } int length() const { return e.text.length(); } StringEntity e;};template<> class wrapper{public: wrapper(const ListEntity &e) : e(e) { } int length() const { return e.elements.size(); } ListEntity e;};template void process(const wrapper &w){ std::cout << "process: " << w.length() << "\n";}int main(){ StringEntity s("hello"); ListEntity l; wrapper se(s); wrapper le(l); process(se); process(le);}Compile time polymorphism, but of completely unrelated types and no use of virtual methods.

My scripting language has both String and List types which are completely unrelated, but have a lot of overlap in terms of the operations the VM needs to do on them.

StringEntity and ListEntity do happen to both inherit a base class, but that is irrelevant for the subject here - could add a specialisation for any other class here.

This approach should mean I can define a common interface without affecting any of the other Entity classes, and use template functions to do the processing.

Call today's entry a snippet since I'm actually at work :)

1 likes 7 comments

Comments

Alberth

Novel use of template specialization :)

December 21, 2016 09:47 AM
Ravyne
It's a useful trick to use template specialization to unify the identifiers so that you can hand unrelated types to another template. It's especially useful when you don't control the types yourself, or can't change the types to unify the names directly. If you do own the types and have the freedom to alter the signatures, it's usually better to do it directly. The C++ STL does that, more or less. Strings are a distinct type, but they're also a container in a sense like std::vector -- you can use them with std::algorithms, for instance. Surprisingly few people seem to know that.
December 21, 2016 04:38 PM
efigenio

So you have just discovered what we had been taught in our first year at college ?

December 22, 2016 11:02 AM
Navyman

If you do own the types and have the freedom to alter the signatures, it's usually better to do it directly. The C++ STL does that, more or less. Strings are a distinct type, but they're also a container in a sense like std::vector -- you can use them with std::algorithms, for instance. Surprisingly few people seem to know that.

Great follow up information!

December 22, 2016 02:01 PM
Bill Door

A similar (but more in-depth) technique I have used is called "Runtime Polymorphism" or originally "Concept-based Polymorphism". This polymorphism is different than inheritance. The technique is described in "Better Code: Runtime Polymorphism" (PDF) and in the video "Value Semantics and Concepts-based Polymorphism" from C++ Now! 2012.

December 22, 2016 05:27 PM
Aardvajk
Thanks for the extra info, all.
December 25, 2016 05:26 PM
Aardvajk

So you have just discovered what we had been taught in our first year at college ?


Yes, even after over twenty five years of using C++, the last five at a professional level, I still find new things. I value humility very highly in a programmer's tool box. Without it, you restrict your own ability to learn and become very difficult to work with. Remind you of anyone?
December 27, 2016 08:45 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement