C++ - std::bind call operator() with perfect forwarding - c++

I have to store arguments (parameter pack), and pass the arguments to another function.
As a result, I cannot use lambda. And a good choice is std::bind.
But for this code
struct A{};
void test(A &&){}
int main()
{
A a;
test(move(a)); //work
bind(test,a)(); //compile fail; copy a to std::bind, pass a to test
}
According to standard, all variables stored in std::bind will be pass as lvalue to function. (The C++ standard doesn't say that, by I think that is what it means.)
And that means I cannot use a function (has rvalue reference in parameter) with std::bind.
One solution is to change test(A &&) to test(A &), but this only works for your project (and make it strange while you not only need to call test by std::thread but also need to call test by plain sequential call).
So, is there any ways to solve this problem?

You can create wrapper which will be convertible to the rvalue reference (like reference_wrapper/l-value references) and use it with bind:
It cal look like that:
#include <iostream>
#include <functional>
struct A{};
void test(A &&){ std::cout << "Works!\n"; }
template <typename T>
struct rvalue_holder
{
T value;
explicit rvalue_holder(T&& arg): value(arg) {}
operator T&&()
{
return std::move(value);
}
};
template <typename T>
rvalue_holder<T> rval(T && val)
{
return rvalue_holder<T>(std::move(val));
}
int main()
{
A a;
test(std::move(a)); //work
auto foo = std::bind(test, rval(std::move(a))); //works
foo();
}
http://coliru.stacked-crooked.com/a/56220bc89a32c860
Note: both rvalue_holder and especially rval need further work to ensure efficiency, robustness and desired behavior in all cases.

Related

Questions on lambda overloads, type conversions and perfect forwarding

This is a question on lambda overload sets and perfect forwarding and somewhat of a followup to a comment. For more context of how this is used see another related question.
I have some questions on the below code snippet.
Q1: For lambda overloads, I was using overload(Fs...) -> overload<Fs...> from this post, but then in this answer I saw overload(Fs&&...) -> overload<std::decay_t<Fs>...>. In what situations is this difference relevant?
Q2: Why would you want to define the identity function below with return decltype(x)(x) and not just return x?
Q3: Can we consider foo(convert(std::forward<Args>(args))...) as perfect forwarding (for all not-converted arguments) just like foo(std::forward<Args>(args)...)?
#include <utility>
#include <iostream>
/////////////////////////////////////////////////////////////////////////////////
struct Foo {
virtual ~Foo() = default;
};
struct FooA: public Foo {
static void foo(const FooA&, int) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
struct FooB: public Foo {
static void foo(int, const FooB&) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
/////////////////////////////////////////////////////////////////////////////////
template<class...Fs>
struct overload:Fs... {
using Fs::operator()...;
};
// Q1: In what situations is needed over `overload(Fs...) -> overload<Fs...>`?
template<class...Fs>
overload(Fs&&...) -> overload<std::decay_t<Fs>...>;
/////////////////////////////////////////////////////////////////////////////////
// Q2: What is the purpose of `return decltype(x)(x)` over `return x`?
auto identity=[](auto&&x)->decltype(x){return decltype(x)(x);};
template<typename SpecificFoo, typename... Args>
void bar(Args&&... args) {
auto convert = overload{
[](const Foo& f){return dynamic_cast<const SpecificFoo&>(f);},
identity
};
// Q3: Is our definition of `convert` "perfectly forwarding", like if we just called
// `foo(std::forward<Args>(args)...)`, or in what situations might this not do the
// same thing (for not-converted types)?
SpecificFoo::foo(convert(std::forward<Args>(args))...);
}
/////////////////////////////////////////////////////////////////////////////////
int main() {
{
FooA specific_foo;
const Foo& foo {specific_foo};
// assume we only have access to foo when calling bar
bar<FooA>(foo, 23);
}
{
FooB specific_foo;
const Foo& foo {specific_foo};
// assume we only have access to foo when calling bar
bar<FooB>(42, foo);
}
}
run it
In what situations is this difference relevant?
When at least one argument is in fact an lvalue (like identity, in fact). In which case the corresponding Fi is a T&, i.e. an lvalue reference. And one can't list an lvalue reference as a base of any class, so std::decay is required to remove all reference and cv-qualifiers. When the deduction guide takes arguments by value, it's automatically a non-issue. This is because template argument deduction for value types already "decays" the types.
If you wonder which one to use, then I'd say the one with less clutter is objectively better. The use of std::decay_t is for getting the same behavior one would get with the by-value version, so one may as well use that.
Why would you want to define the identity function below with return decltype(x)(x) and not just return x
This is a form of forwarding. Since the return type of the lambda is declared to be decltype(x), we need the cast to make sure it binds correctly to an rvalue reference. Because in the case decltype(x) = T&&, it will not bind to x alone, which is an lvalue.
Can we consider foo(convert(std::forward<Args>(args))...) as perfect forwarding (for all not-converted arguments) just like foo(std::forward<Args>(args)...)
Yes. The arguments to bar already bound to references. convert lets those references pass through with value category preserved, so it's forwarding indeed.

How to avoid lambda functions with a known function parameter?

In the following code, I have a variable called data. It holds a functions inside itself to call them later. Let's assume data is defined in another library and I cannot change its type. I assign a template function to each member of it where a portion of this function is known (s3) and a portion must be given when it is called (true). I cannot pass something like this:
data[0]=test_func(?,s3); // error
instead, I have to pass a lambda function to it :
data[0]=[](bool b){test_func(b,s3);}; // ok
But the lambda function does not look neat especially when we have an array of 100 of these assignments. It there any way to avoid lambda functions by just changing the test_func in any way? even using lambda inside test_func is ok to me because it is just written once.
#include <iostream>
#include <functional>
template<typename F>
void test_func(bool b,F f)
{
if(b)
f();
}
void s1()
{
std::cout<<"s1 \n";
}
void s2()
{
std::cout<<"s2 \n";
}
void s3()
{
std::cout<<"s3 \n";
}
int main()
{
test_func(true,s1);
test_func(true,s2);
test_func(false,s1);
test_func(true,s2);
/////////////////
std::function<void(bool)> data[100];
// data=test_func(?,s3); // error
data[0]=[](bool b){test_func(b,s3);}; // ok
data[0](true);
return 0;
}
If you want to avoid a lambda function completely as well as templates you can use a functional (class with operator()):
typedef void (&F)(void);
class TestFunc {
F f;
public:
TestFunc(const F f) : f(f) {}
void operator()(bool B) const {
if(B) f();
}
};
Assign it with TestFunc(s3). Just typedef F to the function type, no need for a template:
typedef void (&F)(void);
and remove the template completely - I usually prefer less templates if possible, but that's taste. A template would only really be called for if you need different function signature support.
To use a standard library functional just change the typedef:
typedef std::function<void(void)> F;
If each s_n is a regular function with an identical signature, you can just remove that f parameter from test_func and instead pass the function itself as template parameter.
template<void(&f)()>
void test_func(bool b)
{
if(b)
f();
}
And use like this:
data[0] = test_func<s1>;
Function pointers and references are explicitly allowed as template non-type parameters by [temp.param/4]:
A non-type template-parameter shall have one of the following
(optionally cv-qualified) types:
[...]
pointer to object or pointer to function,
lvalue reference to object or lvalue reference to function,
You could create your lambda in a helper function:
#include <iostream>
#include <string>
#include <functional>
#include <vector>
template<typename F>
void test_func(bool b,F f) {
if(b) {
f();
}
}
std::function<void(bool)> wrap_function(const std::function<void(void)> &f) {
return [f](bool b){test_func(b,f);};
}
void s1() {
std::cout<<"s1 \n";
}
int main() {
std::vector<std::function<void(bool)>> data;
data.push_back(wrap_function(s1));
data[0](true);
}
And you should use std::vector, std::array or another std container instead of std::function<void(bool)> data[100]

Simple Argument Forwarding (what should my signature be?)

As an example, say I am writing a thin wrapper for a vector's push_back method.
class Foo
{
public:
void myPushBack(Bar b); // Line in question
private:
std::vector<Bar> vec;
}
void Foo::MyPushBack(bar b)
{
vec.push_back(bar);
}
main()
{
Foo f();
f.myPushBack();
}
My question is what is the correct signature for the function myPushBack? Then my next question is what would be the correct signature for the function myPushBack if vec was of type std::vector<weak_ptr<Bar>>?
Assuming that you are using C++11, you should use the perfect forwarding idiom:
template<typename T> void Foo::MyPushBack(T &&b)
{
vec.push_back(std::forward<T>(b));
}
Since it is a template, it does not matter the actual type of the vector. It will even take into account implicit conversions, such as const char* to std::string.

passing rvalue reference into boost::in_place function

I'm new to rvalue references in C++ and want to learn how to use them in everyday life.
I've got 2 connected questions about popular usecases: using rvalue references with boost::in_place and boost::bind.
Using rvalue ref in boost::in_place
Consider a class, with constructor taking rvalue reference as a parameter:
struct A
: boost::noncopyable
{
A(int&&){}
};
Let's now try to create boost optional variable for this class:
void foo(int&& value)
{
boost::optional<A> opt;
// some code here
opt = boost::in_place(std::forward<int>(value)); //Error!
}
What is the right way to pass rvalue ref in such an example. Is there any solution like boost::reference_wrapper for rvalue refs?
passing bind functor to function object with rvalue reference
Another common usecase is assigning boost::bind functor object to boost::function object.
void foo(int&&)
{
}
void bar()
{
boost::function<void(int&&)> func;
int x = 0;
func = boost::bind(foo, std::move(x)); // Compilation error (a)
func = boost::bind(foo, _1); // Compilation error too (b)
}
I understand that instruction (a) could lead to undefined variable value after first invocation, but instruction (b) even has no such a problem. But how to write this code correctly?
boost in_places does not natively support move or r-value references. However, you can make a new class that inherits from boost::in_place_factory_base, and make your own. Or you could use an optional that supports emplace functions (like folly::Optional).
I'm not sure I recommend doing it, but you can make a generic overload in boost to handle it, something like this:
#include <boost/optional.hpp>
#include <boost/utility/in_place_factory.hpp>
#include <folly/ApplyTuple.h>
namespace boost
{
template<class ... As>
struct rvalue_in_place : public in_place_factory_base
{
mutable std::tuple<As...> tup_;
rvalue_in_place(As ... as)
: tup_(std::forward<As>(as)...)
{
}
template< class T >
void apply ( void* address ) const
{
auto make = [address](As ... as) { new (address) T(std::forward<As>(as)...); };
folly::applyTuple(make, tup_);
}
};
template <class ... As>
rvalue_in_place<As&&...> in_place(As && ... a)
{
return rvalue_in_place<As&&...>(std::forward<As>(a)...);
}
}

Passing rvalues through std::bind

I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can't figure out how to do it. For example:
#include <utility>
#include <functional>
template<class Type>
void foo(Type &&value)
{
Type new_object = std::forward<Type>(value); // move-construct if possible
}
class Movable
{
public:
Movable(Movable &&) = default;
Movable &operator=(Movable &&) = default;
};
int main()
{
auto f = std::bind(foo<Movable>, Movable());
f(); // error, but want the same effect as foo(Movable())
}
The reason this fails is because when you specify foo<Movable>, the function you're binding to is:
void foo(Movable&&) // *must* be an rvalue
{
}
However, the value passed by std::bind will not be an rvalue, but an lvalue (stored as a member somewhere in the resulting bind functor). That, is the generated functor is akin to:
struct your_bind
{
your_bind(Movable arg0) :
arg0(arg0)
{}
void operator()()
{
foo<int>(arg0); // lvalue!
}
Movable arg0;
};
Constructed as your_bind(Movable()). So you can see this fails because Movable&& cannot bind to Movable.†
A simple solution might be this instead:
auto f = std::bind(foo<Movable&>, Movable());
Because now the function you're calling is:
void foo(Movable& /* conceptually, this was Movable& &&
and collapsed to Movable& */)
{
}
And the call works fine (and, of course, you could make that foo<const Movable&> if desired). But an interesting question is if we can get your original bind to work, and we can via:
auto f = std::bind(foo<Movable>,
std::bind(static_cast<Movable&&(&)(Movable&)>(std::move<Movable&>),
Movable()));
That is, we just std::move the argument before we make the call, so it can bind. But yikes, that's ugly. The cast is required because std::move is an overloaded function, so we have to specify which overload we want by casting to the desired type, eliminating the other options.
It actually wouldn't be so bad if std::move wasn't overloaded, as if we had something like:
Movable&& my_special_move(Movable& x)
{
return std::move(x);
}
auto f = std::bind(foo<Movable>, std::bind(my_special_move, Movable()));
Which is much simpler. But unless you have such a function laying around, I think it's clear you probably just want to specify a more explicit template argument.
† This is different than calling the function without an explicit template argument, because explicitly specifying it removes the possibility for it to be deduced. (T&&, where T is a template parameter, can be deduced to anything, if you let it be.)
You could use a lambda expression.
auto f = [](){ foo(Movable()); };
This would seem to be the simplest option.
Guys i have hacked up a perfect forwarding version of a binder(limited to 1 param) here
http://code-slim-jim.blogspot.jp/2012/11/stdbind-not-compatable-with-stdmove.html
For reference the code is
template <typename P>
class MovableBinder1
{
typedef void (*F)(P&&);
private:
F func_;
P p0_;
public:
MovableBinder1(F func, P&& p) :
func_(func),
p0_(std::forward<P>(p))
{
std::cout << "Moved" << p0_ << "\n";
}
MovableBinder1(F func, P& p) :
func_(func),
p0_(p)
{
std::cout << "Copied" << p0_ << "\n";
}
~MovableBinder1()
{
std::cout << "~MovableBinder1\n";
}
void operator()()
{
(*func_)(std::forward<P>(p0_));
}
};
As u can see from the above proof of concept, its very possible...
I see no reason why std::bind is incompatible with std::move... std::forward is after all for perfect forwarding I dont understand why there isnt a std::forwarding_bind ???
(This is actually a comment to GMan's answer, but I need some formatting for the code).
If generated functor actually is like this:
struct your_bind
{
your_bind(Movable arg0) :
arg0(arg0)
{}
void operator()()
{
foo(arg0);
}
Movable arg0;
};
then
int main()
{
auto f = your_bind(Movable());
f(); // No errors!
}
compliles without errors. as it's possible to assign and initialize data with rvalue and then pass a data value to rvalue argument of the foo(). However, I suppose that bind implementation extracts function argument type directly from foo() signature. i.e. the generated functor is:
struct your_bind
{
your_bind(Movable && arg0) :
arg0(arg0) // **** Error:cannot convert from Movable to Movable &&amp
{}
void operator()()
{
foo(arg0);
}
Movable&& arg0;
};
and indeed, this really fails to initialize rvalue data member.
Perhaps,the bind implpementation simply does not correctly extract "unreferenced" type from function argument type and uses this type for functor's data member declaration "as is", without trimming &&.
the correct functor should be:
struct your_bind
{
your_bind(Movable&& arg0) :
arg0(arg0)
{}
void operator()()
{
foo(arg0);
}
Movable arg0; // trim && !!!
};
One more improvement in GManNickG's answer and I've got pretty solution:
auto f = std::bind(
foo<Movable>,
std::bind(std::move<Movable&>, Movable())
);
(works in gcc-4.9.2 and msvc2013)