From: Pavel Minaev on
On Apr 11, 1:18 am, sasha <aborovin...(a)gmail.com> wrote:
>
> What are the rules governing virtual member of a template class,
> virtual template member of a template class and template member of non-
> template class?

You cannot have a virtual function template in any kind of class or
class template (because that would require run-time template
instantiation).

You can have virtual functions in class templates. This is not a
problem, since the compiler just has to create a new vtable for every
new instantiation of the class template (and all instantiations of the
class templates are known at link time).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpluslearn on

>
> What are the rules governing virtual member of a template class,
> virtual template member of a template class and template member of non-
> template class?
>

virtual member of a class template - allowed. For example the
destructor can be virtual.
Member function template of a class template - not allowed. Because
unknown instances are possible, but the size of the vtable is fixed.
Member function template of a non-template class - not allowed. same
vtable problem.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Looney on
On Apr 11, 7:18 am, sasha <aborovin...(a)gmail.com> wrote:
> I am bit confused as to whether the following is allowed; why and why
> not:
>
> 1)
> template<typename T>
> class Foo{
>
> ...
> virtual TsomeOp();
>
> };
>
> 2)
>
> template<typename T>
> class Foo{
>
> ...
> template<typename K>
> virtual T someOp(K k);
>
> };
>
> 3)
>
> class Foo{
>
> ...
> template<typename K>
> virtual T someOp(K k);
>
> };
>
> What are the rules governing virtual member of a template class,
> virtual template member of a template class and template member of non-
> template class?

{ edits: quoted sig and banner removed please don't quote extraneous material -mod }

member functions of a c++ class or a class template can be virtual,
but
member function templates of c++ class or a class template can not be
virtual as they do not represent one function, they represent a family
of functions and can have multiple instantiations.

so
template <typename T>
class Foo
{
virtual void a();
virtual void b() = 0;
//a and b are legal declarations and can be virtual as they allways
//resolve to 1 function per the instance of the class type generated
//from the class template

template <typename T2>
virtual void c(){}
// where as c is an illegal definition as c is templated on a
template
// parameter T2 and is member function template, current compilers
can not necessary work out how many
// instances of c are gonna be instantiated across all different
translations
// units have been translated across the whole program, supporting
member function
// templates being virtual would require a whole new kind of
mechanism in c++ compilers and Linkers
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 11 avr, 19:36, Pavel Minaev <int...(a)gmail.com> wrote:

> You cannot have a virtual function template in any kind of class or
> class template (because that would require run-time template
> instantiation).

Not at all, only link-time.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]