Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error assuming cast to type xxx from overloaded function on gcc [duplicate]

Tags:

c++

c++17

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

like image 951
Tyker Avatar asked Mar 11 '19 13:03

Tyker


1 Answers

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?

like image 198
eerorika Avatar answered Oct 24 '22 04:10

eerorika