【发布时间】:2015-07-17 03:05:40
【问题描述】:
受comment 的启发,关于将带有右值引用参数的 lambdas 直接绑定到 std::async,通过 std::async 将右值绑定到 lambda 编译并按预期执行:(live example)
auto lambda = [] (std::string&& message) {
std::cout << message << std::endl;
};
auto future = std::async(lambda, std::string{"hello world"});
future.get();
但是,使用 std::bind 会触发编译器错误:(live example)
auto lambda = [] (std::string&& message) {
std::cout << message << std::endl;
};
auto bound = std::bind(lambda, std::string{"hello world"}); // Compiler error
bound();
这是因为std::bind 将message 保留为左值,因此当它传递给 lambda 时,实参不再匹配参数。
我有read std::async 内部使用std::bind,那么当std::bind 不使用右值引用参数时,它如何摆脱呢?标准中是否有特定部分需要这种行为,或者这取决于编译器?
【问题讨论】:
标签: c++ c++11 lambda rvalue-reference stdbind