I was playing with function pointers vs. std::function and came across the following problem.
Let's consider the folowing code:
#include <cmath>
#include <functional>
// g++ -std=c++17 SF.C -o SF
// clang++ -std=c++17 SF.C -o SF
int main()
{
typedef double (*TpFunctionPointer)(double) ;
TpFunctionPointer pf1 = sin; // o.k.
TpFunctionPointer pf2 = std::sin; // o.k
TpFunctionPointer pf3 = std::riemann_zeta; // o.k
std::function< double(double) > sf1( sin ); // o.k
std::function< double(double) > sf2( std::sin ); // fails
std::function< double(double) > sf3( std::riemann_zeta ); // fails
}
Compiling with g++ v8.2
or clang v7.0
works fine for the function pointer pf1, pf2, pf3, and for sf1.
However for sf2 and sf3 I get a rather long error messages, e.g.:
SF.C:17:47: error: no matching function for call to ‘std::function<double(double)>::function(<unresolved overloaded function type>)’
std::function< double(double)> sf2( std::sin ); // fails
Is this intended behavior?
Shouldn't sf2
and sf3
be fine?