I’d would like to make a method that would perform a certain task plus make a simple calculation from within, like addition, subtraction or multiplication. Correct me if I’m wrong, it seems that I cannot pass the operator of such an operation directly and I need to define an intermediary method (like one in my example called operator_add ). I try to accomplish my task with the following code :
struct A {
typedef int T;
/* (...) */
struct element {
/* (...) */
inline T value() const { /* something simple */ };
element& comp_assign( const T r, T (*operation)(const T, const T) ) { // line # 40
T res = operation( value(), r );
return modif_aux( res );
} /* (...) */
inline T operator_add( const T a, const T b ) { return a + b; }
inline element& operator+=( const T r ) { return comp_assign( r, operator_add ); } // line # 64
};
};
But I get the following error :
A.h:64: error: no matching function for call to ‘A::element::comp_assign(const int&, <unresolved overloaded function type>)’
A.h:40: note: candidates are: A::element& A::element::comp_assign(int, int (*)(int, int))
operator_addis a member function and so you can’t use a normal function pointer to refer to it. Making it a static function or free function would fix this, although I’ll recommend using templates instead as then it can use any callable object: