Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit passing "this" parameter to method call

Tags:

c++

std

c++11

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);

like image 270
user1733773 Avatar asked Dec 06 '13 12:12

user1733773


1 Answers

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)...); }
like image 108
Angew is no longer proud of SO Avatar answered Sep 28 '22 08:09

Angew is no longer proud of SO