After my discovery of incosistency between MSVC and GCC (probably clang too) in compilation and linking of the same code, I've become curious should this program actually compile and link and thus it's bug in MSVC (which reports a linker error) or should I write it differently. The program consist of 3 files:
C.h
template <typename T>
struct A
{
void func() {};
};
template <>
void A<int>::func ();
A.cpp:
#include "C.h"
int main()
{
A<int> x;
x.func();
}
B.cpp:
#include "C.h"
template <>
void A<int>::func()
{
}
The resulting linker error from MSVC is:
A.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::func(void)"
So basically it decides not to create symbol out of definition placed in B.cpp
. The thing which makes me strongly suspect it as a bug is that moving unspecialized definition of func
out of struct definition and even placing it above specialization declaration makes program linnking successful, but I would like to be sure.
So my question is - should this program be compiled and linked without errors by a conformant compiler/linker?
func
- I'm not enough of a language-lawyer to say if that is correct or not (I think it is) – Brachio