Explicitly passing the "this" parameter to a method call

Is this possible in a C ++ call class method with explicit passing of the first parameter "this" param?

Something like that:

struct A { void some() {} }; .... A a; A::some(&a); // ~ a.some(); 

For the reasonable question β€œWHY?”: I need to implement std :: bind analogue, and it works fine with such constructs:

 void f(int); bind(f, 3); 

but this does not work:

 bind(&A::some, &a); 

UPDATE: Guys, my question is obviously not entirely clear. I know how to use std :: bind, I want to know how it handles constructs where this parameter is explicitly passed to it: std :: bind (& A :: some, & a);

+6
source share
2 answers

Here is an idea for a dispatcher that you can 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)...); } 
+5
source

Do you want something like the following?

 struct A { void some(); static void some(A* that) { that->some(); } }; .. A a; A::some(&a); 
0
source

Source: https://habr.com/ru/post/959474/


All Articles