简体   繁体   English

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

[英]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.

相关问题 为什么这段代码可以用 MSVC 编译,但不能在 GCC 或 Clang 中编译? - Why does this code compile with MSVC, but not in GCC or Clang? 为什么以下代码使用 clang 而不是 gcc 编译 - Why does the following code compile using clang but not gcc 为什么下面的代码不能用gcc编译,但用clang编译好 - Why the below code does not compile with gcc but compiles fine with clang 为什么这个代码无法用gcc 4.8.5编译,而它用clang编译好 - Why this code fails to compile with gcc 4.8.5 while it compiles fine with clang 为什么此代码使用g ++而不是gcc进行编译? - Why does this code compile with g++ but not gcc? 无法使用 clang 编译代码,但可以使用 gcc - Cannot compile code with clang, but works with gcc 简单代码不能用 clang 或 gcc 编译,但它可以用 g++ - Simple code will not compile with clang or gcc but it will with g++ 同一段代码,可以在xcode中编译,也可以在终端中使用g ++,clang ++进行编译,而不能使用gcc或clang进行编译。 为什么? - The same piece of code, can compile in xcode or using g++, clang++ in terminal, cannot compile using gcc or clang. Why? 为什么VC ++编译代码而clang没有? - Why does VC++ compile the code while clang doesn't? 为什么VC ++会编译此代码,而Clang不会 - Why does VC++ compile this code while Clang won't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM