Re: Instantiation of virtual member functions of class templates
On 22 Jan, 17:23, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
Nikolay Ivchenkov wrote:
Given the following example:
#include <iostream>
struct B
{
virtual void f() const = 0;
};
template <class T>
struct D : B
{
// overrides B::f
virtual void f() const
{
T()();
}
};
struct F
{
void operator ()() const
{
std::cout << "F::operator()\n";
}
};
int main()
{
D<F> d;
B &b = d;
b.f(); // statically refers to B::f
// dynamically calls D<F>::f
}
is there normative rules that require D<F>::f to be implicitly
instantiated?
Yes, at 3.2p2:
A virtual member function is odr-used if it is not pure.
And 14.7.1p1:
Unless a member of a class template or a member template
has been explicitly instantiated or explicitly specialized,
the specialization of the member is implicitly instantiated
when the specialization is referenced in a context that requires
the member definition to exist;
I can't agree with such answer because I don't see any explicit
reference to D<F>::f (that would appear in some particular context). I
don't think that
"when the specialization is referenced in a context that requires
the member definition to exist"
can be interpreted as
"when the member definition is required to exist".
At 14.7.1p9
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.
I don't see any significant benefits of such freedom, though it can
help programmers to write unreliable code :-)
If we assume that the proposed resolution for issue 230
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#230
should be accepted (it would be interesting to know how many decades
the committee will make a decision about this issue), then
instantiation of definition of a pure virtual function of a class
template specialization may be necessary even if the function is not
odr-used.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]