I have a constructor that accepts a pointer-to-member-function. I have two different functions to be passed in but they fail with some variation of incompatible cv-qualifiers
.
The constructor parameter is
constructor(T (Class::* memfnptr)())
where T
and Class
are typenames.
My two member functions are of the following types when passed as arguments.
long int (Encoder::*)() const
const char* (Drivebase::*)()
I have tried many combinations of const qualifiers in the following positions, plus a reference const V (Class::* const func)() const
, const V (Class::* const & func)() const
.
I can get either one of my functions to work, but not both. What am I doing wrong?
You cannot deduce the const
-qualifier of the member function pointer in template argument deduction, so you need two overloads:
template<typename T, typename Class>
constructor(T (Class::* memfnptr)());
template<typename T, typename Class>
constructor(T (Class::* memfnptr)() const);
The first one will be called for member functions which are not const
qualified and the second will be called for those that are.
Note that there are more qualifiers for member functions with the same problem: volatile
, &
and &&
You may not need to accept all of these since they are uncommon, but to get a full set you need 12 overloads.