How to Directly Bind a Member Function to an Std::Function in Visual Studio 11

How to directly bind a member function to an std::function in Visual Studio 11?

I think according to the C++11 standard, this should be supported

Not really, because a non-static member function has an implicit first parameter of type (cv-qualified) YourType*, so in this case it does not match void(int). Hence the need for std::bind:

Register(std::bind(&Class::Function, PointerToSomeInstanceOfClass, _1));

For example

Class c;
using namespace std::placeholders; // for _1, _2 etc.
c.Register(std::bind(&Class::Function, &c, _1));

Edit You mention that this is to be called with the same Class instance. In that case, you can use a simple non-member function:

void foo(int n)
{
theClassInstance.Function(n);
}

then

Class c;
c.Register(foo);

Using generic std::function objects with member functions in one class

A non-static member function must be called with an object. That is, it always implicitly passes "this" pointer as its argument.

Because your std::function signature specifies that your function doesn't take any arguments (<void(void)>), you must bind the first (and the only) argument.

std::function<void(void)> f = std::bind(&Foo::doSomething, this);

If you want to bind a function with parameters, you need to specify placeholders:

using namespace std::placeholders;
std::function<void(int,int)> f = std::bind(&Foo::doSomethingArgs, this, std::placeholders::_1, std::placeholders::_2);

Or, if your compiler supports C++11 lambdas:

std::function<void(int,int)> f = [=](int a, int b) {
this->doSomethingArgs(a, b);
}

(I don't have a C++11 capable compiler at hand right now, so I can't check this one.)

Passing non-static member function as std::function

You can write a lambda capturing this, on which passedFunction could be called.

calledFunction([this](int v) { this->passedFunction(v); });

C++17 Cannot use std::bind to produce a std::function

Your function Register expects a function with two parameters, but you try to pass to it a function with one placeholded parameter.

void TestRegister() {
Register(
std::bind(&TestClass::TestTarget, this, std::placeholders::_1, std::placeholders::_2)
);
}

std::function to member function

Funcp func = 
std::bind(&A::func, &a, std::placeholders::_1, std::placeholders::_2);

How do I bind a member function template passing unspecified call wrapper

The root cause of the problem seems to be the internal copying of its arguments performed by std::bind, with particular reference to t.

You may want to workaround it this way:

  template <typename T>
explicit X(T t)
{
std::bind(&X::invoke<T>, this, std::placeholders::_1)(t);
// ^^^^^^^^^^^^^^^^^^^^^ ^
}

This will also work, but you won't be allowed to make the result of bind outlive the argument t (otherwise, you would be passing a dangling reference to invoke<T>()):

  template <typename T>
explicit X(T t)
{
std::bind(&X::invoke<T>, this, cref(t))();
// ^^^^^^^
}

UPDATE:

The above solutions are workarounds that help for achieving what you are showing in your example. However, it emerged from the comments that your use case may be quite different (e.g. passing around the result of bind for later evaluation).

As correctly pointed out by Maxim Yegorushkin in his answer, the conceptually correct solution consists in using a construct such as Boost's protect.

In case you do not want to use Boost, it is quite easy to define your own protect() function using C++11's variadic templates:

// Imitates boost::protect, but with variadic templates and perfect forwarding
namespace detail
{
template<typename F>
struct protect
{
private:

F _f;

public:

explicit protect(F f): _f(f)
{
}

template<typename... Ts>
auto operator () (Ts&&... args) ->
decltype(_f(std::forward<Ts>(args)...))
{
return _f(std::forward<Ts>(args)...);
}
};
}

template<typename F>
detail::protect<F> protect(F&& f)
{
return detail::protect<F>(std::forward<F>(f));
}

Eventually, this is how you could use it in your class, as Maxim suggested:

class X
{
public:
template <typename T>
explicit X(T t)
{
auto pt = protect(t);
std::bind(&X::invoke<decltype(pt)>, this, pt)();
}
private:
template <typename T>
void invoke(T t)
{
t();
}
};

Get a c++11 function wrapper for a member function without using std::bind

As a member of Signal:

template<typename C>
ConnectionHandle connect( C* c, RetType(C::*func)(Args...)) {
return connect( [c,func](Args&&...args) {
return (c->*func)( std::forward<Args>(args)... );
});
}

template<typename C, typename Method, typename... Unused>
ConnectionHandle connect( C* c, Method* m, Unused&&...) {
return connect( [c,func](Args&&...args) {
return (c->*m)( std::forward<Args>(args)... );
});
}

The first overload gives you the ability to distinguish between overloaded methods.

The second, if the first fails, lets you use signature-compatible methods.

The ... portion makes sure the first is a better match if they both match (I hope!)

Args&&... should work with a std::function, assuming the std::function properly perfect forwards. If not, replace with Args....

I see no need for bind or variants, given that we are going to stuff it into a std::function immediately anyhow. Create a lambda, they are well suited to the task.

C++ How do you pass a member function into a functor parameter?

You can use a lambda function with a capture.

dds::sub::cond::ReadCondition rc(*mp_reader,
dds::sub::status::DataState::any(),
[this](){ this->do_stuff(); });


Related Topics



Leave a reply



Submit