Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is making a function template specialization virtual legal?

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.

like image 420
alexk7 Avatar asked Apr 16 '09 17:04

alexk7


1 Answers

Nice compiler error. For this type of checks I always fallback to the Comeau compiler before going back to the standard and checking.

Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 3: error: "virtual" is not allowed in a function template declaration template virtual void f(); ^

"ComeauTest.c", line 10: error: "virtual" is not allowed in a function template declaration template virtual void f(); ^

Now, as it has been posted by another user, the fact is that the standard does not allow you to define virtual templated methods. The rationale is that for all virtual methods, an entry must be reserved in the vtable. The problem is that template methods will only be defined when they have been instantiated (used). This means that the vtable would end up having a different number of elements in each compilation unit, depending on how many different calls to f() with different types happen. Then hell would be raised...

If what you want is a templated function on one of its arguments and one specific version being virtual (note the part of the argument) you can do it:

class Base
{
public:
   template <typename T> void f( T a ) {}
   virtual void f( int a ) { std::cout << "base" << std::endl; }
};
class Derived : public Base
{
public:
   virtual void f( int a ) { std::cout << "derived" << std::endl; }
};
int main()
{
   Derived d;
   Base& b = d;
   b.f( 5 ); // The compiler will prefer the non-templated method and print "derived"
}

If you want this generalized for any type, then you are out of luck. Consider another type of delegation instead of polymorphism (aggregation + delegation could be a solution). More information on the problem at hand would help in determining a solution.

like image 85
David Rodríguez - dribeas Avatar answered Oct 12 '22 23:10

David Rodríguez - dribeas