I'm trying to write a template function, but I have trouble specializing it for vector<> and another class at the same time. Here is the code I'm using :
// template definition
template< class T >
void f( const T& value)
{
cout << "DEFAULT" << endl;
}
// specialization for MyClass
template< class T >
void f<> ( const MyClass & value )
{
cout << "MyClass" << endl;
}
// specialization for vector
template< class T >
void f<>( const std::vector<T> & v )
{
cout << "vector" << endl;
}
MyClass
and MyClass2
are defined as:
class MyClass{
virtual void a() = 0;
};
class MyClass2 : public MyClass{
void a(){}
};
Finally, the main
function:
int main(int nArgs, char *vArgs[]){
MyClass2 e;
f<MyClass>(e);
}
Here is the error I get when I try compiling it using Visual Studio 2010:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(869): error C2259: 'MyClass' : cannot instantiate abstract class
This seems to be very specific to this particular situation: As soon as I remove the const
modifiers, I change the vector
into a list
or I make MyClass
concrete, everything works. But for my problem I need for this particular situation to work.
Is anybody having the same error as me, and more importantly does anybody know a fix/workaround for this?