Consider this as an extension of my previous question:
How to pass a member function pointer to an overloaded method in a template function?
I have a function which receives pointer to class method:
template<typename Return, typename T>
T ReceiveFuncPtr (Return (T::*Method)(const int&))
{
T obj;
(obj.*Method)(1);
return obj;
}
It works fine for a handwritten class such as:
template<typename X, typename = void>
struct A
{
std::pair<X,bool> foo (const X& i) { return std::pair<X,bool>(X(),true); } // choice
void foo (std::initializer_list<X> i) { return 0; }
};
int main ()
{
ReceiveFuncPtr(&A<int>::foo); // OK
}
However, I am unable to pass std::set<int>::insert(const int&)
ReceiveFuncPtr(&std::set<int>::insert);
Above statement results in:
error: no matching function for call to ‘ReceiveFuncPtr(<unresolved overloaded function type>)’
ReceiveFuncPtr(&std::set<int>::insert); // ERROR
^
note: candidate: template<class Return, class T> T ReceiveFuncPtr(Return (T::*)(const int&))
T ReceiveFuncPtr (Return (T::*Method)(const int&))
^
note: template argument deduction/substitution failed:
note: mismatched types ‘const int&’ and ‘std::initializer_list<int>’
ReceiveFuncPtr(&std::set<int>::insert); // ERROR
^
note: mismatched types ‘const int&’ and ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
note: mismatched types ‘const int&’ and ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
note: mismatched types ‘const int&’ and ‘std::set<int>::value_type&& {aka int&&}’
note: couldn't deduce template parameter ‘Return’
What is the correct way to pass such method?