我遇到了好奇的问题。 给定模板类和模板函数:
template<typename L, typename T>
class TemplateClass
{
public:
int operator()()
{
return 1;
}
};
template<typename K, template<typename L,
typename T = int> class TemplateCl>
TemplateCl<K> getTemplateClass(TemplateCl<K> _cls)
{
TemplateCl<K> cls;
return cls;
}
int main()
{
auto templateClass = TemplateClass<int,int>();
auto rTemplateClass = getTemplateClass<int>(templateClass);
return 0;
}
此代码由Clang编译,但当我尝试使用VS2015进行编译时,它失败了:
Severity Code Description Project File Line Suppression State
Error C2672 'getTemplateClass': no matching overloaded function found cpp11_cpp14_cpp17 d:\projects_programing\__testing\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17.cpp 682
Severity Code Description Project File Line Suppression State
Error C2893 Failed to specialize function template 'TemplateCl<K,int> getTemplateClass(TemplateCl<K,int>)' cpp11_cpp14_cpp17 d:\projects_programing\__testing\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17.cpp 682
此代码仅通过以下修改进行编译:
template<typename L, typename T = int>
class TemplateClass
{
public:
int operator()()
{
return 1;
}
};
最奇怪的情况是它甚至可以通过以下修改来编译:
template<typename L, typename T = float>
class TemplateClass
{
public:
int operator()()
{
return 1;
}
};
这是VS2015编译器的问题吗? 有谁知道为什么VS2015有这么奇怪的行为?