views:

1013

answers:

5

The question is pretty much fully embedded in the title.

+3  A: 

Compile time polymorphism applies to functions and operator overloads.

Read this http://cpp-tutorial.cpp4u.com/OOP%5Fpolymorphism.html

TP
+5  A: 

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.

anon
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.
nmr
@ Neil I agree with you. Class templates also seem to exibit compile time polymorphism.
captonssj
+10  A: 

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.

Andreas Brinck
damn, beat me to it.
wheaties
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
@captonssj, I would say that yes, they do.
Omnifarious
+9  A: 

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.

Jerry Coffin
The 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
+4  A: 

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.

Steve Jessop
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<typename _Tx>std::mem_fun_t<_Tx> mem_fun(const _Tx }
Jasper Bekkers
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