The impact of virtual on the use of member of class template

后端 未结 1 1773
孤街浪徒
孤街浪徒 2021-02-05 16:44

I (vaguely) know that a template is not instantiated if it is not used. For example, the following code will compile fine even though T::type

1条回答
  •  野性不改
    2021-02-05 17:36

    A quick look at 3.2 [basic.def.odr] yields:

    3/ [...] A virtual member function is odr-used if it is not pure. [...]

    And I also found at 14.7.1 [temp.inst]:

    10/ 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. (emphasis mine)

    So... I would say it is likely that a virtual method will always be instantiated.

    In pragmatic terms, I would expect a compiler to instantiate the virtual table of a template class when it instantiates the class; and thus immediately instantiate all virtual member functions of this class (so it can references those from the virtual table).

    0 讨论(0)
提交回复
热议问题