• entries
743
1924
• views
583688

Compile time "polymorphism"

2143 views

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 :)

Novel use of template specialization :)

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.

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

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.

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.

Thanks for the extra info, all.

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?

Create an account

Register a new account