我在MSVC 2015 RC上偶然发现了一个奇怪的行为,我无法理解在LLVM3.4上完美运行的代码。
我有一个类层次结构,其中包含一些模板类,它接受非成员函数作为模板参数,所以像这样(我修剪了所有不相关的代码)
// .h file
namespace functions {
float func1(float x) { return 0.0f; }
float func2(float x) { return 0.0f; }
float func3(float x) { return 0.0f; }
}
class Base
{
public:
virtual Base* clone() const = 0;
virtual ~Base() { }
};
using Func = float(*)(float);
template<Func F, Func I = F>
class Derived : public Base
{
public:
Derived() { }
~Derived() { }
static Derived<F,I>* create();
Base* clone() const override;
};
using MyDerived = Derived<functions::func1>;
using MyDerived2 = Derived<functions::func2, functions::func3>;
// .cpp file
template<Func F, Func I>
Base* Derived<F,I>::clone() const
{ // <- all errors point here
return new Derived<F,I>();
}
template<Func F, Func I>
Derived<F,I>* Derived<F,I>::create()
{
return new Derived<F,I>();
}
template class Derived<functions::func1>;
template class Derived<functions::func2, functions::func3>;
现在,这在Clang上工作正常但在MSVC上我遇到了奇怪的错误,如
C2062: type 'float' unexpected
C2976: 'Derived': too few template arguments
C2955: use of class template requires template argument list
C2910: Derived<F,I>::clone: cannot be explicitly specialized
它们看起来不合乎逻辑,尤其是第一个错误,所以我想知道它是否是一个MSVC错误或Clang允许某些东西不应该被允许即使我没有&#39找不到。
这些错误看起来像是从源文件中模板的显式实例化生成的:
template class Derived<functions::func1>;
template class Derived<functions::func2, functions::func3>;