I have the following functor and its partial specialisation
template <class _T, typename _Return = void, typename _Arg = void>
struct Caller
{
typedef _Return(_T::*Method)(_Arg);
Caller(Method pm, _Arg a)
: _pMethod(pm),
_arg(a)
{}
_Return operator()(_T& obj)
{
return (obj.*_pMethod)(_arg);
}
Method _pMethod;
_Arg _arg;
};
template <class _T, typename _Return>
struct Caller<_T, _Return, void>
{
typedef _Return(_T::*Method)();
Caller(Method pm)
: _pMethod(pm)
{}
_Return operator()(_T& obj)
{
return (obj.*_pMethod)();
}
Method _pMethod;
};
I'm trying to use it the following way:
struct Foo
{
void Bar() const
{
void(0);
}
};
// ...
std::list<Foo> foos;
const std::list<Foo> &rFoos(foos);
std::for_each(rFoos.begin(), rFoos.end(), Caller<const Foo>(&Foo::Bar));
I'm getting this, for the last line of code (the IDE picks up at the Caller):
error C2440: '' : cannot convert from 'void (__thiscall Foo::* )(void) const' to 'Caller<_T>' 1> with 1> [ 1> _T=const Foo 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous
This code would work in a g++ environment. (If I Caller<Foo>(&Foo::Bar)
g++ would complain, which makes sense, as the function will only be called on a const object).
I have tried various things, including adding operator()(const _T& obj)
/ operator()(const _T& obj) const
varieties to the functor, but to no avail.
This would be accepted by the compiler:
struct Foo
{
void Bar()
{
void(0);
}
};
// ...
std::list<Foo> foos;
const std::list<Foo> &rFoos(foos);
std::for_each(rFoos.begin(), rFoos.end(), Caller<Foo>(&Foo::Bar));
What am I doing wrong? How can I make the functor template work for const member functions in Visual C++?