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, 2022 in C++ by Nicholas
• 7,760 points
702 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 Jun 20, 2022 by Damon
• 4,960 points

Related Questions In C++

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, 2022 in C++ by Damon
• 4,960 points
868 views
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, 2022 in C++ by Nicholas
• 7,760 points
545 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, 2022 in C++ by Damon
• 4,960 points
492 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 Jun 21, 2022 in C++ by Damon
• 4,960 points
689 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, 2022 in Others by Damon
• 4,960 points
1,272 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, 2022 in C++ by Damon
• 4,960 points
702 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, 2022 in C++ by Damon
• 4,960 points
611 views
0 votes
1 answer

Why we actually need runtime polymorphism?

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

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,289 views
0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
986 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 Jun 21, 2022 in C++ by Damon
• 4,960 points
492 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP