As you can see from here not all template functions are being compiled in a template class. Standard says, that if the method or a member is not being used, it is not being compiled. Cool feature, indeed! Now, lets discuss the following hierarchy:
class A
{
virtual void f() {};
};
template <typename T>
class B : public A
{
virtual void f () override
{
// do something dangerous
}
}
You will never know if B<int>::f
is being called, right? The reason is that you can call a function B::f
with dynamic binding and you never know if A*
points onto B
type object or other typed object that is derived from A
. So how the compiler should handle this case?
A* ptr = nullptr;
if (i > 0)
{
ptr = new B<int>();
}
ptr->f();
How does compiler guess/spot this case to generate B<int>::f
IMPORTANT RELATED QUESTION: Can a C++ class member function template be virtual?