The following code does not compile:
#include <functional>
template<class ...Args>
void invoke(Args&&... args)
{
}
template<class ...Args>
void bind_and_forward(Args&&... args)
{
auto binder = std::bind(&invoke<Args...>, std::forward<Args>(args)...);
binder();
}
int main()
{
int a = 1;
bind_and_forward(a, 2);
}
If I understand correctly, the reason is as follows: std::bind
copies its arguments, and when the binder
's operator()
is called, it passes all the bound arguments as lvalues - even those ones that entered bind
as rvalues. But invoke
was instantiated for the original arguments, and it can't accept what the binder
attempts to pass it.
Is there any solution for this problem?