[英]Why does this code compile with gcc but not with clang
This code works perfectly fine with gcc/g++ and msvc but not with clang. 这段代码与gcc / g ++和msvc完全兼容,但与clang无关。 It keeps complaining that no matching function for Log was found, what is going on?
它一直抱怨没有找到Log的匹配功能,发生了什么?
#include <iostream>
template <typename Function, typename... Args>
auto Call(Function func, Args&&... args) -> typename std::result_of<Function&(Args&&...)>::type
{
return func(std::forward<Args>(args)...);
}
template <typename T, typename... Args>
T (*Log( T (*FuncPtr)(Args...) ))(Args...)
{
return FuncPtr;
}
int main()
{
auto r = Log(Call<int(int), int>)([](int x){
return x*10;
}, 10);
std::cerr << r << std::endl;
}
Error: 错误:
> error: no matching function for call to 'Log'
> auto r = Log(Call<int(int), int>)([](int x){
> ^~~ test7.cpp:15:5: note: candidate template ignored: couldn't infer template argument 'T' T (*Log( T (*FuncPtr)(Args...)
> ))(Args...)
> ^ 1 error generated.
I believe that this code is incorrect. 我相信这段代码不正确。 The function parameter to
Log
cannot be used for template argument deduction in this case because the argument is a non-deduced context. 在这种情况下,
Log
的函数参数不能用于模板参数推导,因为参数是非推导的上下文。
From [temp.deduct.type] in the standard, p5 lists the non-deduced contexts, and p5.5 says: 从标准中的[temp.deduct.type]开始,p5列出了非推导的上下文,p5.5表示:
A function parameter for which argument deduction cannot be done because the associated function argument is a function, or a set of overloaded functions (13.4), and one or more of the following apply:
由于关联的函数参数是函数或一组重载函数(13.4)而无法进行参数推导的函数参数,以及以下一个或多个适用:
and p5.5.3 says: 和p5.5.3说:
the set of functions supplied as an argument contains one or more function templates.
作为参数提供的函数集包含一个或多个函数模板。
My interpretation is that you have a function parameter for which the function argument is a (pointer to) a function and that function is a function template. 我的解释是你有一个函数参数,函数参数是函数的一个(指向),函数是一个函数模板。
Arguably, because this isn't an overload set, this might be something that could be allowed in the future, but I read the standard as not guaranteeing that this technique will work. 可以说,因为这不是一个重载集,这可能是未来可以允许的,但我读标准并不能保证这种技术能够正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.