Function Pointer Overloading Error?
-
19-06-2021 - |
Question
I have the following definitions and prototypes (which are member functions of a class) with which I basically try to use function pointers to pass a different type of strategy to the divideQuery method:
typedef vector<ConstraintManager> (*strategyType1)(const Query&);
typedef vector<ConstraintManager> (*strategyType2)(const Query&, int);
vector<ConstraintManager> divideQuery (strategyType1 s, const Query& query);
vector<ConstraintManager> divideQuery (strategyType2 s, const Query& query, int parts);
vector<ConstraintManager> divideByHalf(const Query& query);
vector<ConstraintManager> divideRandom(const Query& query);
vector<ConstraintManager> divideByN(const Query& query, int n);
However when I try to call (query
parameter is passed from the wrapping function):
vector<ConstraintManager> result = divideQuery(divideRandom, query);
It fails with the error message:
DividingSolver.cpp:200:70: error: no matching function for call to ‘DividingSolver::divideQuery(<unresolved overloaded function type>, const klee::Query&)’
DividingSolver.cpp:82:27: note: candidates are: std::vector<klee::ConstraintManager> DividingSolver::divideQuery(std::vector<klee::ConstraintManager> (*)(const klee::Query&), const klee::Query&)
DividingSolver.cpp:87:27: note: std::vector<klee::ConstraintManager> DividingSolver::divideQuery(std::vector<klee::ConstraintManager> (*)(const klee::Query&, int), const klee::Query&, int)
As far as I read from the web it seems like an overloading error (or maybe something else) but in any case I am not quite sure what exactly is wrong. Any hints/explanations is appreciated.
Cheers
Solution
The problem is that a non-static member function does not have the same signature as a free function. In particular it has a hidden this
pointer. Now what you have is a call to divideQuery
where the first argument is a pointer to member function, but there is no overload that takes pointer to member function, so the compiler bails out with an error message.
If the functions do not use the state from the class, you can make them static
, that will remove the implicit this
and it should work. Another option is actually using pointers to member in the divideQuery
interface. The third option would be using a higher level construct, like std::function<>
(or boost::function<>
) for the function argument, and then using std::bind
(boost::bind
) to bind the this
argument.
OTHER TIPS
Make divideRandom
static, then it will be compatible with a normal function pointer.