I specialized a template and encountered a different beahviour between the MSVC compiler and MinGW/GCC. Here is the header file:
// MyClass.h
#ifndef MYCLASS_HEADER
#define MYCLASS_HEADER
template<typename T>
class MyClass {
public:
virtual void doSomething() {
// some code
}
};
// specialization prototype here
#endif
Now, the difference. To avoid multiple definitions, i specialized only a prototype in the header file, the implementation is in a cpp file. The specialization is the problem. the GCC/MinGW compiler accepts only this one:
template<> void MyClass<int>::doSomething(); // GCC version
And MSVC only this:
extern template void MyClass<int>::doSomething(); // MSVC version
The implementation in the CPP file is the same for both:
// MyClass.cpp
#include "MyClass.h"
template<> void MyClass<int>::doSomething() {
// some code
}
with the GCC prototype the MSVC compiler throws an "unresolved external symbol" error, GCC with the MSVC version throws "specialization of ... after instantiation".
At the moment, i have the workaround:
#ifdef _MSC_VER
extern template
#else
template<>
#endif
void MyClass<int>::doSomething();
in the header file. It works, but i don't like it. Is there a way to avoid the compiler specific switch?