1

的VisualStudio不能编译这个代码(错误C2976)VS2015错误C2976

但GCC和锵可以编译该代码

为什么???

#include <iostream> 
#include <map> 

template <typename... ARGS> 
void Func(const std::map<ARGS...>& m) 
{ 
    //... 
} 

template <typename T> 
void Func(const T& t) 
{ 
    //... 
} 

int main() 
{ 
    std::map<int, double> m; 
    Func(m); // error C2976: 'std::map': too few template arguments 
    Func(123); // OK 
    return 0; 
} 
+1

它显示任何错误? –

+1

[std:map作为模板参数的模板扣除失败]的可能重复(http://stackoverflow.com/questions/26059219/template-deduction-fails-for-stdmap-as-template-parameter) – cromod

回答

1

我的猜测是,这是因为的Visual Studio 2015年不完全支持嵌套的可变参数模板,也不能推断出正确的类型。

这样的工作,你身边有明确指定的类型,所以您可以使用Func<std::map<int, double>>(m);Func<int, double>(m);甚至Func<decltype(m)>(m);(我推荐的最后一个)。