gcc 中未解析的重载函数类型
Unresolved overloaded function type in gcc
我正在尝试将一个函数模板作为参数传递给另一个函数,如下例所示。
#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 中以某种方式规避这个吗?
神箭link:https://godbolt.org/z/oCChAT
是的,这应该是 a bug of gcc, which has not been fixed even in gcc 10.0.1。
当用 placeholder type specifiers like auto
and decltype(auto)
. If you specify the return type as T
it would work fine 指定 return 类型时,gcc 似乎无法处理这种情况。
对于 gcc,另一种解决方法是:
auto f = foo<decltype(arg)>;
std::cout << bar(f, arg) << std::endl;
我正在尝试将一个函数模板作为参数传递给另一个函数,如下例所示。
#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 中以某种方式规避这个吗?
神箭link:https://godbolt.org/z/oCChAT
是的,这应该是 a bug of gcc, which has not been fixed even in gcc 10.0.1。
当用 placeholder type specifiers like auto
and decltype(auto)
. If you specify the return type as T
it would work fine 指定 return 类型时,gcc 似乎无法处理这种情况。
对于 gcc,另一种解决方法是:
auto f = foo<decltype(arg)>;
std::cout << bar(f, arg) << std::endl;