i hit an error on gcc that doesn't occur on clang
template<typename T>
int invoke_func(T a) {
return a;
}
bool test(int(*ptr)(int))
{
return ptr == invoke_func<int>;
}
godbolt
error:
<source>: In function 'bool test(int (*)(int))':
<source>:9:19: error: assuming cast to type 'int (*)(int)' from overloaded function [-fpermissive]
return ptr == invoke_func<int>;
^~~~~~~~~~~~~~~~
is gcc right to reject this code ?
after further testing replacing invoke_func<int>
by &invoke_func<int>
works on gcc and clang.
but why is the &
required here when it isn't in expression like int(*ptr)(int) = invoke_func<int>;
?
godbolt
There is no overload resolution involved, so message is certainly a GCC bug at the very least. There appears to be a bug report: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81059
Edit: When searching for standard rule, I stumbled upon an existing answer to a duplicate, which explains the corner case: Why can't one compare a function pointer to a template function without explicit & on function name?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With