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() { }
A.obj : error LNK2019: unresolved external symbol “public: void __thiscall A::func(void)”
所以基本上决定不要在B.cpp中创建不符合定义的符号.让我强烈怀疑它是一个错误的事情是,从结构定义中移除未定义的func,甚至将其放在专业化声明之上,使程序linnking成功,但我想确定.
解决方法
© ISO/IEC N4527
14.6.4.1 Point of instantiation [temp.point] 1 For a function template specialization,a member function template specialization,or a
specialization for a member function or static data member of a class
template,if the specialization is implicitly instantiated because it
is referenced from within another template specialization and the
context from which it is referenced depends on a template parameter,
the point of instantiation of the specialization is the point of
instantiation of the enclosing specialization. Otherwise,the point
of instantiation for such a specialization immediately follows the
namespace scope declaration or definition that refers to the
specialization.
在这种情况下,我认为这是在C.h那里发生“范围声明”的意思.如果是这种情况,那么您的代码应该与标准兼容的工具链链接.我可能会误解这个…