What is compile-time polymorphism and why does it only apply to functions?
-
There is some contradiction in my textbook and as of yet no answer from an instructor. I figured SO may have an answer. – ihtkwot Dec 10 '09 at 14:55
-
2Can you describe the contradiction you see? - Maybe that helps answering the actual question you have. – johannes Dec 10 '09 at 14:56
-
1There is some ambiguity in my textbook, or I'm misunderstanding what I'm reading, so I figured I ask here. – ihtkwot Dec 10 '09 at 14:57
-
I guess classes would be the only other thing I could think of but that doesn't seem right. – ihtkwot Dec 10 '09 at 15:07
-
@nmr I agree. Compiler does choose which version of the class to instantiate based on the type T we provide, doesn't this mean class templates also exibit compile time polymorphic behaviour ? – captonssj Dec 10 '09 at 15:16
5 Answers
Way back when, "compile time polymorphism" meant function overloading. It applies only to functions because they're all you can overload.
In current C++, templates change that. Neil Butterworth has already given one example. Another uses template specialization. For example:
#include <iostream>
#include <string>
template <class T>
struct my_template {
T foo;
my_template() : foo(T()) {}
};
template <>
struct my_template<int> {
enum { foo = 42 };
};
int main() {
my_template<int> x;
my_template<long> y;
my_template<std::string> z;
std::cout << x.foo << "\n";
std::cout << y.foo << "\n";
std::cout << "\"" << z.foo << "\"";
return 0;
}
This should yield 42
, 0
, and ""
(an empty string) -- we're getting a struct that acts differently for each type.
Here we have "compile time polymorphism" of classes instead of functions. I suppose if you wanted to argue the point, you could claim that this is at least partially the result of the constructor (a function) in at least one case, but the specialized version of my_template
doesn't even have a constructor.
Edit: As to why this is polymorphism. I put "compile time polymorphism" in quotes for a reason -- it's somewhat different from normal polymorphism. Nonetheless, we're getting an effect similar to what we'd expect from overloading functions:
int value(int x) { return 0; }
long value(long x) { return 42; }
std::cout << value(1);
std::cout << value(1L);
Function overloading and specialization are giving similar effects. I agree that it's open to some question whether "polymorphism" applies to either, but I think it applies about equally well to one as the other.

- 476,176
- 80
- 629
- 1,111
-
1The term compile time polymorphism is a bit vague, can you elaborate a little more on in what way you think your example is polymorphic? I'm still straddling the fence on wether this is or isn't actually polymorphism. I would perhaps argue that the polymorphic part of this example is the overloading of operator `<<` for `std::ostream`. Should one think of the template instantiation as a sort of abstract function where the type is the argument? – Andreas Brinck Dec 10 '09 at 17:05
-
How would you argue that polymorphism does not apply? Polymophism meaning either "many forms". In you examples (and in funciton overloading) the same function selector results in several different "forms" of the function. – Rune FS May 13 '14 at 06:49
With compile time polymorphism one usually means the fact that you can have a several functions with the same name and the compiler will choose at compile time which one to used depending on the arguments:
void foo(int x);
void foo(float y);
//Somewhere else
int x = 3;
foo(x); //Will call first function
float y = 2;
foo(y); //Will call second function
The function foo
is said to be overloaded. Various types of template instantiation could also be called compile time polymorphism.

- 51,293
- 14
- 84
- 114
-
What about class templates ? Compiler does choose which version of the class to instantiate based on the type T we provide, so does this mean class templates also exibit compile time polymorphic behaviour ? – captonssj Dec 10 '09 at 15:12
-
-
Is this more of an runtime polymorphism example? I guess I get confused by the difference btw the two, i.e. runtime polymorphism and compile time polymorphism...sigh... – JustADude Oct 01 '12 at 19:47
-
The code in my example is compile time, i.e. which overloaded function that should be called is determined at compile time. – Andreas Brinck Oct 03 '12 at 08:44
Compile time polymorphism is a term that refers to C++ template programming. For example, at compile time you determine the actual type of a std::vector by what it contains:
std::vector <int> vi;
std::vector <std::string> vs;
I'm not sure why you think it it is limited to functions.
-
Mostly because I was told in my class it was limited to functions. I don't fully understand the why it is limited to only functions. – ihtkwot Dec 10 '09 at 15:00
-
1@ Neil I agree with you. Class templates also seem to exibit compile time polymorphism. – captonssj Dec 10 '09 at 15:15
The thing which only applies to functions is template parameter deduction. If I have a function template:
template <typename T>
void foo(T &t);
Then I can do int a = 0; foo(a);
, and this will be equivalent to int a = 0; foo<int>(a);
. The compiler works out that I mean foo<int>
. At least, it works out that it should use foo<int>
- if that's not what I meant then bad luck to me, and I could have written foo<unsigned int>(a);
or whatever.
However, if I have a class template:
template <typename T>
struct Foo {
T &t;
Foo(T &t) : t(t) {}
T &getT() { return t; }
};
Then I can't do int a = 0; Foo(a).getT();
. I have to specify Foo<int>(a)
. The compiler isn't allowed to work out that I mean Foo<int>
.
So you might say that class templates are "less polymorphic" than function templates. Polymorphism usually means that you don't have to write code to make the type of your object explicit. Function templates allow that (in this particular case), and class templates don't.
As for why this is the case - the standard says so, I don't know why. The usual suspects are (a) it's too difficult to implement, (b) it's not useful, in the opinion of the standard committee, or (c) it creates some contradiction or ambiguity somewhere else in the language.
But you can still do other kinds of polymorphism with classes:
template <typename T>
struct Foo {
T &t;
Foo(T &t): t(t) {}
void handleMany(int *ra, size_t s) {
for (size_t i = 0; i < s; ++i) {
t.handleOne(ra[i]);
}
}
};
This is usually also called compile-time polymorphism, because as far as the author of the template is concerned, t.handleOne
could be anything, and what it is will be resolved when necessary, "later" in the compilation when Foo is instantiated.

- 273,490
- 39
- 460
- 699
-
I think it's useful according to the standard committee, because they chose to work around this specific problem with the implementation of std::mem_fun / std::mem_fun_t. Basically what they did was: template
std::mem_fun_t<_Tx> mem_fun(const _Tx &t) { return std::mem_fun_t<_Tx>(t); } – Jasper Bekkers Dec 10 '09 at 16:26 -
Yes, and the same with iterator adaptors: `back_inserter` returns a `back_insert_iterator`. When I said "and", I meant "this is the list", rather than "these are all true". I think (a) is unlikely too, so it's either (c) or "no good reason". – Steve Jessop Dec 10 '09 at 16:57
Compile time polymorphism applies to functions and operator overloads.
Read this http://cpp-tutorial.cpp4u.com/OOP_polymorphism.html

- 6,203
- 15
- 56
- 72