了解 C++11 的 Lambda 中的移动捕获

Understanding Move Capture in Lambdas for C++11

我对为解决 C++11 lambda 中的移动捕获而提出的解决方法有疑问。特别是,以 Meyer's book 中的示例为例:

std::vector<double> data;
... 
auto func = std::bind( [](const std::vector<double>& data) 
                       { /*uses of data*/ },
                       std::move(data)
                     );

我的问题是:将参数 "data" 声明为右值引用的 consequences/meaning 是什么?:

auto func = std::bind( [](std::vector<double>&& data)
...

为了帮助您指导答案,我将提出三点主张。请告诉我我是对还是错:

提前致谢。

what would be the consequences/meaning of declaring the parameter "data" as an rvalue reference?

它不会编译(至少如果您尝试实际调用 func)。 std::bind 始终将绑定参数作为左值传递,这不会绑定到右值引用。

In both cases, it is not safe to use data after the definition of "func".

data 因移动而处于有效但未指定的状态。您可以像使用内容未知的向量一样使用它。