为什么这段代码是用gcc编译的,而不是用clang编译的

Why does this code compile with gcc but not with clang

本文关键字:编译 clang 段代码 gcc 代码 为什么      更新时间:2023-10-16

此代码与gcc/g++和msvc配合使用非常好,但与clang配合使用则不然。它一直抱怨找不到与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: 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.

我认为这段代码是不正确的。在这种情况下,Log的函数参数不能用于模板参数推导,因为该参数是非推导上下文。

根据标准中的[temp.dexpract.type],p5列出了未推导的上下文,p5.5表示:

无法进行参数推导的函数参数因为关联的函数参数是一个函数,或者一组过载功能(13.4),并且以下一个或多个适用:

和p5.5.3说:

作为参数提供的函数集包含一个或多个函数模板。

我的解释是,您有一个函数参数,其函数参数是一个函数(指向),而该函数是一个功能模板。

可以说,因为这不是一个过载集,所以这可能是未来允许的,但我认为标准并不能保证这种技术会起作用。