------- Comment #11 from herwig at gdsys dot de 2008-04-02 07:17 -------
(In reply to comment #10)
> Yes. Since the class declaration must be visible from the place where you
> call this function, and since then also the function's definition
> (=implementation) is visible, the template should be instantiated at the
> place where you call the member function. Is this not the case?
No, it is not. And that's because this pure virtual method never gets called
explicitly.
I think the compiler is taking an illegal shortcut in assuming a pure virtual
template method never gets called. When deriving a class from the template, the
vtable is built and it surely has a slot for pvMethod() because it's a virtual
method. So the template is (fully?) instantiated at the point, where I derive
from it. See the following example:
template<class T>
class TBase
{
public:
virtual void pvMethod() = 0;
virtual void vMethod();
};
template<class T>
void TBase<T>::pvMethod()
{
}
template<class T>
void TBase<T>::vMethod()
{
}
class TDerived :
public TBase<TDerived>
{
public:
void pvMethod();
};
void TDerived::pvMethod()
{
}
int main(int argc, char** argv)
{
return 0;
}
There is no instance of TDerived whatsoever. But looking at the binary reveals
there is even a TBase<TDerived>::vMethod():
[EMAIL PROTECTED]:~/Programming$ g++ -O0 -Wall test.cpp -o test && objdump -t
test |
c++filt | grep '\(TBase\|TDerived\)'
08048474 g F .text 00000005 TDerived::pvMethod()
08048570 w O .rodata 0000000a typeinfo name for
TDerived
0804857c w O .rodata 0000000c typeinfo for TDerived
08048560 w O .rodata 00000010 vtable for TDerived
08048590 w O .rodata 00000012 typeinfo name for
TBase<TDerived>
08048494 w F .text 00000005 TBase<TDerived>::vMethod()
08048588 w O .rodata 00000008 typeinfo for
TBase<TDerived>
Now, in my opinion there must be a TBase<TDerived>::pvMethod(), because
(a) it is legal to specify a fallback implementation,
(b) the slot is surely there, and
(c) as we found out, there is no workaround for it.
Perhaps (c) is a false conclusion because you or I missed something, or (a) is
wrong concerning templates, but nevertheless it's annoying that there's no
clean solution for it. In my code I solved it by making the pure virtual method
virtual only. But now there has been at least one occasion where I forgot to
provide an implementation in a derived class...
If you think my reasoning is right, would you mind reopening the bug?
Best regards,
Björn!
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33878