Note when using std::forward
First of all, std::forward designed for perfect redirection , that is, for redirecting a reference type (l-value or r-value).
If you pass the l-value reference to std::forward , that is, what is returned, and also if the r-value reference is passed, the r-value is returned. This works unlike std::move , which will always return an r-value reference. Also remember that the named r-value values ββare l-value references.
/* Function that takes r-value reference. */ void func(my_type&& t) { // 't' is named and thus is an l-value reference here. // Pass 't' as lvalue reference. some_other_func(t); // Pass 't' as rvalue reference (forwards rvalueness). some_other_func(std::forward<my_type>(t)); // 'std::move' should be used instead as we know 't' is always an rvalue. // eg some_other_func(std::move(t)); }
Also, you should never use std::forward or std::move for an object, after which you need to access some state. Moved objects are placed in an indefinite, but valid state, which basically means that you cannot do anything with them except to destroy or reassign the state for them.
Arguments passed by std::bind ?
The std::bind function will always copy or move its arguments. I could not find a suitable quote from the standard, but ru.cppreference.com says :
"Bind arguments are copied or moved, and never passed by reference unless they are enclosed in std :: ref or std :: cref."
This means that if you pass the argument as a reference to the l-value, then it will be created with a copy, and if you pass it as a reference to the r-value, then it will be created. In any case, the argument will never be passed as references.
To get around this, you can, for example, use std::ref as a copied link shell that will internally contain a link to a variable on the call site.
auto func = std::bind(&Foo::test, std::ref(f));
Or you can just pass a pointer to f , as you suggest.
auto func = std::bind(&Foo::test, &f);
What happens, then std::bind will reference the r-value link to the temporary pointer returned from the call to the address operator to f . This pointer will be copied (because the pointers cannot be moved) to the related shell object returned from std::bind . Despite the fact that the pointer itself is copied, it will still point to an object on the call site, and you will get the required link semantics.
Alternatively, use lambda , which captures f by reference and calls the Foo::test function. This is the IMHO recommended approach, since lambdas are more universal and powerful than the std::bind expressions in general.
Foo f; fi = 100; auto func = [&f] { f.test(); }; fi = 1000; func(); // 1000
Note: for a good explanation of when to use std::forward see this great video from Scott Meyers .