为什么代码被clang接受而被vc++拒绝?
Why is the code accepted by clang but rejected by vc++?
clang 3.4接受以下代码;而vc++ NOV 2013 CTP拒绝它,并出现一个错误:
error C2668: 'AreEqual' : ambiguous call to overloaded function
template<class headT, class... tailTypes>
constexpr headT&& __GetFirst__(headT&& value, tailTypes&&...)
{
return static_cast<headT&&>(value);
};
template<class T>
constexpr bool AreEqual(const T& a, const T& b)
{
return a == b;
}
template<class headT, class... tailTypes>
constexpr bool AreEqual(const headT& head_value, const tailTypes&... tail_values)
{
return AreEqual(head_value, __GetFirst__(tail_values...))
&& AreEqual(tail_values...);
}
int main()
{
AreEqual(1, 1, 2, 1);
}
哪个编译器是正确的按照c++ 14标准?
更新:完整的错误信息:
error C2668: 'AreEqual' : ambiguous call to overloaded function
1> d:projectsktltestmain.cpp(20): could be 'bool AreEqual<headT,int>(const headT &,const int &)'
1> with
1> [
1> headT=int
1> ]
1> d:projectsktltestmain.cpp(8): or 'bool AreEqual<headT>(const T &,const T &)'
1> with
1> [
1> headT=int
1> , T=int
1> ]
1> while trying to match the argument list '(const int, const int)'
1>
1>Build FAILED.
Clang(和GCC)的行为是正确的。你可以阅读标准§14.8.2.4 [temp. deduction .partial],了解如何对函数模板进行偏排序,但该子条款p8给出的示例直接涵盖了这种情况:
template<class... Args> void f(Args... args); // #1
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
f(); // calls #1
f(1, 2, 3); // calls #2
f(1, 2); // calls #3; non-variadic template #3 is more
// specialized than the variadic templates #1 and #2
相关文章:
- 访问被拒绝后,c++中的故障保护代码
- 参考资源文件VC++中的$(SolutionDir)
- VC++本机单元测试,找不到调试符号
- IpOpt拒绝解决不受约束的问题
- Termux权限被拒绝
- AWS Lambda C++运行时权限被拒绝
- WinSock2:connect() 提供"连接被拒绝"
- 为什么 KMS drmModeSetCrtc() 在 X11 会话中运行时会失败并被拒绝权限?
- 删除目录函数访问被拒绝
- 模板签名解析为 void(void) 被 GCC 拒绝;这是否有效C++?
- 为什么 Windows 拒绝访问某些进程的名称?
- std::stoi 的版本拒绝任何非数字
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 我在执行任何程序时被拒绝在 devcpp 中访问
- VS2015 中的 VC++ 目录
- Windows C++:文件夹移动访问被拒绝错误
- VS2015/VC++ 在新类模板中禁用默认 #include "stdafx.h"
- AWS AMI 中的 Windows DPAPI 失败,访问被拒绝
- 为什么VC++2013拒绝编译嵌套类型,当用作模板函数返回类型时,用using关键字使其可见
- 为什么代码被clang接受而被vc++拒绝?