Something like, for example,
class A {
template<typename T> T DoStuff();
template<> virtual int DoStuff<int>() = 0;
};
Visual Studio 2010 says no, but I get a funny feeling that I simply messed up the syntax. Can explicit full specializations of a member function template be virtual?
No, template member functions cannot be virtual.
An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.
The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.
You can choose to specialize only some of the parameters of a class template. This is known as partial specialization. Note that function templates cannot be partially specialized; use overloading to achieve the same effect.
Explicit specializations aren't legal within a class. Even if you could make it a partial specialization you would still run into the "templates can't be virtual" problem.
n3290, § 14.5.2 states:
A member function template shall not be virtual.
And gives this example:
template <class T> struct AA {
template <class C> virtual void g(C); // error
virtual void f(); // OK
};
Before going on to state that member function templates do not count for virtual overrides too.
According to C++98 Standard member function template shall not be virtual. http://www.kuzbass.ru:8086/docs/isocpp/template.html.
-3- A member function template shall not be virtual. [Example:
template <class T> struct AA { template <class C> virtual void g(C); // error virtual void f(); // OK };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With