I know that the normal member function of a template class will be instantiated whenever it is used for the first time. But this cannot be done for the 开发者_如何学运维virtual member function as it can be accessed through a base class pointer. Does this mean that virtual member functions will be instantiated as soon as the template class is instantiated? If not, when?
14.7.1/9
in C++03:
An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class or a static data member of a class template that does not require instantiation. It is unspecified whether or not an implementation implicitly instantiates a virtual member function of a class template if the virtual member function would not otherwise be instantiated.
It is implementation defined.
But usually when a template class is instantiated all member for the new class type are generated.
According to the C++ Standard 14.6.4.1/4:
If a virtual function is implicitly instantiated, its point of instantiation is immediately following the point of instantiation of its enclosing class template specialization.
精彩评论