Reputation: 360
Is it possible in c++ call class method with explicit passing first "this" param to it?
Something like this:
struct A
{
void some() {}
};
....
A a;
A::some(&a); // ~ a.some();
For reasonable question "WHY?": i need to implement std::bind analogue, and it works fine with constructions like this:
void f(int);
bind(f, 3);
but this doesn't work:
bind(&A::some, &a);
UPDATE: Guys, my question is obviously not really clear. I know how to use std::bind, i want to know HOW is it processing constructions where this param explicitly passed to it: std::bind(&A::some, &a);
Upvotes: 6
Views: 380
Reputation: 217145
Do you want something like the following ?
struct A
{
void some();
static void some(A* that) { that->some(); }
};
..
A a;
A::some(&a);
Upvotes: 0
Reputation: 171117
Here is an idea for a dispatcher which you could use inside your bind
:
template <class R, class... Arg>
R call(R (*f)(Arg...), Arg &&... arg)
{ return f(std::forward<Arg>(arg)...); }
template <class C, class R, class... Arg>
R call(R (C::*f)(Arg...), C &c, Arg &&... arg)
{ return (c.*f)(std::forward<Arg>(arg)...); }
Upvotes: 5