I am trying to call a function pointer using an explicit dereference. But the compiler throws an error:
no operator "*" matches these operands.
Here's a simplified version of my code:
#include <functional>
#include <iostream>
int add(int a, int b)
{
return a + b;
}
std::function<int(int, int)> passFunction()
{
return &add;
}
int main()
{
int a{ 1 };
int b{ 2 };
std::cout << (*passFunction())(a, b);
return 0;
}
The thing is, it works fine when I just write:
std::cout << passFunction()(a, b); // without asterix.
which blows my mind.
I thought that, I messed up parentheses in function call. I tried different order and precedence, I called it with ampersand, and still compiler doesn't even flinch.