Thursday, 22 August 2013

How to combine std::bind(), variadic templates, and perfect forwarding?

How to combine std::bind(), variadic templates, and perfect forwarding?

I want to invoke a method from another, but both use variadic templates.
For example:
struct foo
{
template <typename... Args>
void invoke(int n, Args&&... args)
{
auto bound = std::bind(&foo::invoke_impl<Args...>, this,
std::placeholders::_1,
std::foward<Args>(args)...);
bound(n);
}
template <typename... Args>
void invoke_impl(int, Args&&...)
{
}
};
foo f;
f.invoke(1, 2);
Problem is, I get a compilation error:
/usr/include/c++/4.7/functional:1206:35: error: cannot bind 'int' lvalue
to 'int&&'
I tried using a lambda, but it seems that GCC 4.8 does not handle the
syntax yet.
From what I understand, the compiler wants to instantiate invoke_impl with
type int&&, while I thought that using && in this case would preserve the
actual argument type.
What am I doing wrong? Thanks,

No comments:

Post a Comment