[英]Clang “ couldn't infer template argument ” whereas gcc / g++ can. Which is right?
I have been trying to compile a project (which is fine using gcc/g++) with clang and compilation stuck on a template invocation. 我一直在尝试编译一个项目(使用gcc / g ++很好),其中clang和compilation卡在模板调用上。 I've tried to create the simplest similar piece of code exhibiting the same error message. 我试图创建最简单的类似代码,显示相同的错误消息。 Here it is: 这里是:
#include <vector>
#include <utility>
#include <iostream>
using namespace std;
int A( double in )
{
return 1;
}
int A( int in )
{
return 1;
}
template<class M, class T>
M test(T input, M (fun) (T) )
{
return fun( input );
}
int main( int argc, const char *argv[] )
{
cout << test( (int) 1, A ) << test( (double) 1.2, A ) << endl;
return 0;
}
The error from clang (appears twice of course): 来自clang的错误(当然会出现两次):
error: no matching function for call to 'test'
candidate template ignored: couldn't infer template argument 'M'
Gcc doesn't complain. Gcc不抱怨。 Please note M is the return type and is always "int". 请注意M是返回类型,始终为“int”。
Does someone know which is right and why? 有人知道哪个是对的,为什么?
Thanks 谢谢
g++ is wrong. g ++错了。 From C++11 [temp.deduct.type]p5: 从C ++ 11 [temp.deduct.type] p5:
The non-deduced contexts are: [...] - A function parameter for which argument deduction cannot be done because the associated function argument is [...] a set of overloaded functions, and [...] more than one function matches the function parameter type 非推导的上下文是:[...] - 无法进行参数推导的函数参数,因为关联的函数参数是一组重载函数,以及[...]多个函数匹配函数参数类型
This determination is made without regard to template parameters that might have been deduced elsewhere, so the fact that T
must deduce as int
is not relevant here. 这个确定是在不考虑可能在其他地方推断出的模板参数的情况下进行的,因此T
必须推导为int
的事实在这里是不相关的。 This makes the entire parameter M (fun)(T)
a non-deduced context. 这使得整个参数M (fun)(T)
成为非推导的上下文。 Therefore M
cannot be deduced, just as Clang claims. 因此,正如Clang声称的那样, M
无法推断出来。
g++ appears to incorrectly be using the ' T
= int
' deduction from the first function parameter when determining whether the second parameter is a non-deduced context. 在确定第二个参数是否是非推导的上下文时,g ++似乎错误地使用了来自第一个函数参数的' T
= int
'推论。 Reversing the order of the function parameters causes g++ to reject the code too: 反转函数参数的顺序会导致g ++拒绝代码:
int A(double);
int A(int);
template<class M, class T>
M test(M (fun) (T), T input) {
return fun( input );
}
int main( int argc, const char *argv[]) {
test(A, 1);
test(A, 1.2);
}
My previous answer (now deleted) was wrong. 我之前的回答(现已删除)是错误的。 Clang is wrong. Clang错了。
The compiler should be able to deduce the type M
because the function argument is M(fun)(T)
. 编译器应该能够推导出类型M
因为函数参数是M(fun)(T)
。 Note that there is no M
in the function pointer argument list, so this corresponds to the T()
(C++11) / T(*)()
(C++93) in 14.8.2.5: 请注意,函数指针参数列表中没有M
,因此这对应于14.8.2.5中的T()
(C ++ 11)/ T(*)()
(C ++ 93):
where
(T)
represents a parameter-type-list where at least one parameter type contains aT
, and()
represents a parameter-type-list where no parameter type contains aT
. 其中(T)
表示参数类型列表 ,其中至少一个参数类型包含T
,()
表示参数类型列表 ,其中没有参数类型包含T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.