What is compile-time polymorphism and why does it only apply to functions?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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:
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:
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.
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:
The function
foo
is said to be overloaded. Various types of template instantiation could also be called compile time polymorphism.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:
I'm not sure why you think it it is limited to functions.
The thing which only applies to functions is template parameter deduction. If I have a function template:
Then I can do
int a = 0; foo(a);
, and this will be equivalent toint a = 0; foo<int>(a);
. The compiler works out that I meanfoo<int>
. At least, it works out that it should usefoo<int>
- if that's not what I meant then bad luck to me, and I could have writtenfoo<unsigned int>(a);
or whatever.However, if I have a class template:
Then I can't do
int a = 0; Foo(a).getT();
. I have to specifyFoo<int>(a)
. The compiler isn't allowed to work out that I meanFoo<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:
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.Compile time polymorphism applies to functions and operator overloads.
Read this http://cpp-tutorial.cpp4u.com/OOP_polymorphism.html