The Four Polymorphisms in C++
catonmat.net
catonmat.net
I'm always disappointed if people only know one kind of polymorphism and apply that rigorously to all problems. Overviews like this might help people to make a conscious decision on which language construct would be a good fit for their actual design problem.
Look at any on-line forums discussing C++. If you're a bit more cynical, look at a list of tricky C++ interview questions instead. :-)
You'll find discussions about overloading vs. overriding for class member functions. You'll find discussions of which function gets called when you have both overloaded functions and function templates to consider. You'll find discussions of which sequences of implicit conversions are allowed, and via what mechanisms.
A very high proportion of the nasty edge cases for which C++ has become infamous are caused by trying to combine different forms of polymorphism. I'm all for knowing your tools and evaluating all the options, but I can see why in practice a lot of teams do stick almost entirely to (for example) using classes/OOP and avoiding templates.
void foo(float f);
int main()
{
foo(1);
return 0;
}
What name is polymorphic here? I suppose you can say that foo is polymorphic if you attribute the coercion operation to foo, but that's not the way I think about it. foo is called after the coercion. The coercion is caused by the relation of the type of 1 with the type of the function parameter f.In your blog post it would have made things much clearer if you had acknowledged the conventional usage of the term in the context of C++ and explained the different definition you were using.
[1] "On Understanding Types, Data Abstraction, and Polymorphism" http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf
http://www.oriontransfer.co.nz/blog/2009-05/what-is-abstract...
http://www.oriontransfer.co.nz/blog/2009-05/run-time-polymor...
http://www.oriontransfer.co.nz/blog/2009-05/compile-time-pol...
If you find it interesting that is great. I enjoyed this blog post.
http://www.reddit.com/r/programming/comments/cgckf/the_four_...
Macros certainly allow you to write code that's applied to differently typed arguments.
FOO.hxx:
TYPE FOO_TEMPLATE () {
TYPE foo;
/*...*/
return foo;
}Foo.h:
#DEFINE FOO_TEMPLATE foo_int
#DEFINE TYPE int
#include "Foo.hxx"
#DEFINE FOO_TEMPLATE foo_float
#DEFINE TYPE float
#include "Foo.hxx"
It's a kind of weird and ugly macro trick, bug it can be useful in C when one's need polymorphism and performance (and cannot use C++ at all, which is a far better solution).
[Edit:] I realize now it's not polymorphism like said in the article, because the caller had to tell explicitly which foo_xxx to call. But it did provide "polymorphic" code in Foo.hxx (so only one implementation, no repetition). That said, a cast is also explicit.