Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When the virtual member functions of a template class instantiated?

Tags:

c++

templates

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?

like image 532
chappar Avatar asked Feb 27 '23 11:02

chappar


2 Answers

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.

like image 137
Johannes Schaub - litb Avatar answered Mar 08 '23 23:03

Johannes Schaub - litb


It is implementation defined.

But usually when a template class is instantiated all member for the new class type are generated.

like image 37
Martin York Avatar answered Mar 08 '23 23:03

Martin York