I am trying to pass a function template as an argument to another function as seen in the example below.
#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;
}
While this works in clang 9.0 and msvc v19.24 it fails with gcc 9.2
gcc output:
no matching function for call to 'bar(<unresolved overloaded function type>, int&)' std::cout << bar(foo<decltype(arg)>, arg) << std::endl;
Is this a bug in gcc? Also can i circumvent this somehow in gcc?
Godbolt link: https://godbolt.org/z/oCChAT
For gcc another workaround can be :
auto f = foo<decltype(arg)>;
std::cout << bar(f, arg) << std::endl;
Yes, this is supposed to be a bug of gcc , which has not been fixed even in gcc 10.0.1 .
It seems gcc fails to handling this case when specifying the return type with placeholder type specifiers like auto
and decltype(auto)
. If you specify the return type as T
it would work fine .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.