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

0 votes
What what is compile-time polymorphism, and why is it limited to functions?
Jun 15 in C++ by Nicholas
• 2,060 points
9 views

1 answer to this question.

0 votes

"Compile time polymorphism" used to signify function overloading. 

It only applies to functions because that's all you can overload.

Templates in modern C++ modify this. 

One example has previously been provided by Neil Butterworth. 

Another technique is template specialisation. 

As an 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 return 42, 0, and "" (an empty string) – we're receiving a struct that behaves differently depending on the type.

Instead of functions, we have "compile time polymorphism" of classes. 

You might argue that this is at least partially the effect of the function Object() { [native code] } (a function) in at least one example, but the specialised form of my template doesn't even have a function Object() { [native code] }.

Edit: To clarify why this is polymorphism. 

I placed "compile time polymorphism" in quotes since it differs from standard polymorphism. 

Nonetheless, the result is identical to what we would 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 specialisation have comparable results. 

I agree that it's debatable whether "polymorphism" applies to either, but I believe it does in roughly equal measure to both.

answered 6 days ago by Damon
• 3,380 points

Related Questions In C++

0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1 in C++ by Nicholas
• 2,060 points
17 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 3,380 points
10 views
0 votes
1 answer

C++ auto keyword. Why is it magic?

Auto was a keyword that C++ "inherited" from C and had been around for a long time but was almost never used because there were only two possibilities: it wasn't allowed or it was assumed by default. C++11 introduced the usage of auto to denote an inferred type. Similarly to how template type deduction works for function templates, auto x = initializer deduces the type of x from the type of initializer.  Consider the following function template: template<class T> int whatever(T t) { ...READ MORE

answered 5 days ago in C++ by Damon
• 3,380 points
12 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27 in Others by Damon
• 3,380 points
9 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7 in C++ by Damon
• 3,380 points
14 views
0 votes
1 answer

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

answered Jun 7 in C++ by Damon
• 3,380 points
18 views
0 votes
1 answer

Why we actually need runtime polymorphism?

One of the most significant elements of ...READ MORE

answered Jun 10 in C++ by Damon
• 3,380 points
9 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered 5 days ago in C++ by Damon
• 3,380 points
16 views
0 votes
1 answer

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

answered May 31 in C++ by Damon
• 3,380 points
31 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP