Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is compile-time polymorphism and why does it only apply to functions?

Tags:

What is compile-time polymorphism and why does it only apply to functions?

like image 658
ihtkwot Avatar asked Dec 10 '09 14:12

ihtkwot


People also ask

What is a compile time polymorphism?

What Is Compile-time Polymorphism? Compile-time polymorphism is obtained through method overloading. The term method overloading allows us to have more than one method with the same name. Since this process is executed during compile time, that's why it is known as Compile-Time Polymorphism.

Why do we need compile time polymorphism?

Compile Time Polymorphism: Whenever an object is bound with its functionality at the compile time, this is known as the compile-time polymorphism. At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism or static or early binding.

What are runtime and compile time polymorphism explain with an example?

Its is a concept by which we can perform single task in multiple ways. There are two types of polymorphism one is Compile-time polymorphism and another is run-time polymorphism. Method overloading is the example of compile time polymorphism and method overriding is the example of run-time polymorphism.

Why function overloading is compile time polymorphism?

Function overloading means one function can perform many tasks. In C++, a single function is used to perform many tasks with the same name and different types of arguments. In the function overloading function will call at the time of program compilation. It is an example of compile-time polymorphism.


1 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.

like image 176
Jerry Coffin Avatar answered Sep 21 '22 05:09

Jerry Coffin