plenty of great examples of maps of function pointers to static and non-static members - I have not been able to find one which uses class templates as well.
I'm having trouble understanding this error message when using the code below:
tclass.H:34:16: error: cannot convert ‘int (*)(MyPair*)’
to ‘std::map, int (MyPair::*)(MyPair*), std::less >, std::allocator,
int (MyPair::*)(MyPair*)> > >::mapped_type {aka int (MyPair::*)(MyPair*)}’
in assignment
f[ "max" ] = &MyPair::max;
Code in question is:
#include <map>
using namespace std;
template <class T>
class MyPair {
public:
MyPair (T first, T second) :
a( first ), b( second ) {}
static T max( MyPair* );
T call( const string& name ) {
func f = s_funcs[ name ];
return ( this->*f )( this );
}
protected:
typedef T ( MyPair::*func ) ( MyPair<T>* );
typedef map<string, func> FuncMap;
static FuncMap initMap();
static FuncMap s_funcs;
T a, b;
};
template <class T>
typename MyPair<T>::FuncMap MyPair<T>::s_funcs = initMap();
template <class T>
typename MyPair<T>::FuncMap MyPair<T>::initMap() {
FuncMap f;
f[ "max" ] = &MyPair<T>::max;
return f;
}
template <class T>
T MyPair<T>::max ( MyPair<T>* pair ) {
T retval;
retval = pair -> a > pair -> b ?
pair -> a :
pair -> b;
return retval;
}