繁体   English   中英

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

[英]Why does this code compile with gcc but not with clang

这段代码与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.deduct.type]开始,p5列出了非推导的上下文,p5.5表示:

由于关联的函数参数是函数或一组重载函数(13.4)而无法进行参数推导的函数参数,以及以下一个或多个适用:

和p5.5.3说:

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

我的解释是你有一个函数参数,函数参数是函数的一个(指向),函数是一个函数模板。

可以说,因为这不是一个重载集,这可能是未来可以允许的,但我读标准并不能保证这种技术能够正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM