function pointer and function template (typedef , template<typename>)

 

Hello ladies and gentlemen ! 

Is it the case that typedef is static and we can´t use it with templates or am I using the wrong syntax here ? 

There is no problem when i using pre defined datatypes like this:    

typedef bool (*Function_pointer)(int, int);
Function_pointer  p_func; 
bool p_more(int a, int b) { return a > b; }
bool p_less(int a, int b) { return a < b; }

p_func = p_more;
Print(p_func(10,5));
p_func = p_less;
Print(p_func(10,5));	

The problem arises when I try to use the function pointer together with the template like this: 

template <typename T>
typedef bool (*Function_pointer_1)(T, T);
Function_pointer p_func; 
template <typename T>
bool p_more(T a, T b) { return a > b; }
template <typename T>
bool p_less(T a, T b) { return a < b; }

p_func = p_more;
Print(p_func(10,5));
p_func = p_less;
Print(p_func(10,5));

now i get compiler error: cannot cast function. If this cannot be done, what would be the best minimalistic solution to the same problem? Would it be reasonable to create minimalistic classes for every function and then pass whole necessary classes using a pointer ? ...or what would be the best lightweight solution here ? 


Documentation on MQL5: Checkup / Point
Documentation on MQL5: Checkup / Point
  • www.mql5.com
Returns the point size of the current symbol in the quote currency. Return Value The value of the _Point variable which stores the point size of...
 
titaanlabidas:

Hello ladies and gentlemen ! 

Is it the case that typedef is static and we can´t use it with templates or am I using the wrong syntax here ? 

There is no problem when i using pre defined datatypes like this:    

The problem arises when I try to use the function pointer together with the template like this: 

now i get compiler error: cannot cast function. If this cannot be done, what would be the best minimalistic solution to the same problem? Would it be reasonable to create minimalistic classes for every function and then pass whole necessary classes using a pointer ? ...or what would be the best lightweight solution here ? 


OK. i found myself solution already. This seems legit approach. Just please confirm when you have time that there is no easier and we can´t do that without classes... ?

class Func {};
typedef bool(*FPointer)(Func &foo);
 
titaanlabidas #:

OK. i found myself solution already. This seems legit approach. Just please confirm when you have time that there is no easier and we can´t do that without classes... ?

Viable solution (creative workaround).

Congrats, your work is commendable!

Reason: