Alex Rivera | Logout

Is making a function template specialization virtual legal?

Asked 2009-04-16T17:45:13.810
16

In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual?

For example:

struct A
{
    template <class T> void f();
    template <> virtual void f<int>() {}
};

struct B : A
{
    template <class T> void f();
    template <> virtual void f<int>() {}
};

int main(int argc, char* argv[])
{
    B b;
    A& a = b;
    a.f<int>();
}

Visual Studio 2005 gives me the following error:

fatal error C1001: An internal error has occurred in the compiler.

Edit
Report

1 Answer

4

According to http://www.kuzbass.ru:8086/docs/isocpp/template.html ISO/IEC 14882:1998:

-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
};
answered 2009-04-16T18:08:47.653

Your Answer