【问题标题】:Unresolved overloaded function type in gccgcc中未解决的重载函数类型
【发布时间】:2020-06-23 07:27:37
【问题描述】:

我正在尝试将函数模板作为参数传递给另一个函数,如下例所示。

#include <iostream>

template <typename T>
decltype(auto) foo(T t)
{
    return t;
}

template <typename Fn, typename T>
decltype(auto) bar(Fn fn, T t)
{
    return fn(t);
}

int main()
{
    int arg = 0;

    std::cout << bar(foo<decltype(arg)>, arg) << std::endl;

    return 0;
}

虽然这在 clang 9.0 和 msvc v19.24 中有效,但它在 gcc 9.2 中失败

gcc 输出:

no matching function for call to 'bar(<unresolved overloaded function type>, int&)' std::cout << bar(foo<decltype(arg)>, arg) << std::endl;

这是 gcc 中的错误吗?我也可以在 gcc 中以某种方式规避这个问题吗?

天箭链接:https://godbolt.org/z/oCChAT

【问题讨论】:

    标签: c++ templates gcc c++14 function-templates


    【解决方案1】:

    是的,这应该是a bug of gcc,即使在gcc 10.0.1中也没有修复。

    当使用placeholder type specifiers (如autodecltype(auto))指定返回类型时,gcc 似乎无法处理这种情况。如果您将返回类型指定为T,它将是work fine

    【讨论】:

      【解决方案2】:

      对于 gcc,另一种解决方法是:

      auto f = foo<decltype(arg)>;
      std::cout << bar(f, arg) << std::endl;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-19
        • 1970-01-01
        • 2013-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多