我想将一个rvalue传递std::bind给一个在C++ 0x中采用rvalue引用的函数.我无法弄清楚该怎么做.例如:
#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())
}
Run Code Online (Sandbox Code Playgroud) 我试图存储一个前向功能std::function.如果我使用std::bind,我收到错误消息no viable conversion from ....如果我使用lambda,它编译好了.
这是示例代码
#include <functional>
template<typename Handler>void func1(int a, Handler&& handler) {}
template<typename Handler>void func2(Handler&& handler)
{
// this line compile fine
std::function<void ()> funcA = [handler = std::move(handler)]() { func1(1, std::move(handler)); };
// this line got compile error
std::function<void ()> funcB = std::bind(func1<Handler>, 1, std::move(handler));
}
int main()
{
func2(&main); // this just a sample, I am using functor as argument in real code
}
Run Code Online (Sandbox Code Playgroud)
尝试g ++ --std = …