Converting template function pointer to bool fails
Consider the code:
void fnc(int, long, double*){}
template<int I> void f(int, long, double*){}
int main()
{
bool a = fnc; //(1) ok
bool b = f<4>; //(2) error
}
It throws an error :
error: cannot resolve overloaded function 'f' based on conversion to type 'bool'
bool b = f<4> //(2) error
Why is the first case correct, but the second wrong?
+3
source to share
1 answer
You completely ignore all the warnings that should tell you that you are doing something very bad.
Also, you must use &
a function to get the address.
Second, you are implicitly casting a pointer to a variable bool
.
Throw this explicitly to tell the compiler that you think you know what you are doing and you are sure of it:
bool b= (void*)&ff<4>;
I just have to say that casting to avoid mistakes and warnings is a bad idea.
In most cases, warnings and errors help you avoid data loss and things that usually cause runtime errors.
+1
source to share