c++ - std::move 为 "in-place operation"

标签 c++ c++11 move move-semantics

这可能与其他问题非常相似;我环顾四周,但我不太清楚自己在说什么。

我正在编写一个“应该”就地的函数,但它是由 BLAS 调用实现的。 BLAS 调用不在位,所以我需要做一个临时的。因此:

void InPlace(ArrayClass& U, const TransformMatrix* M){
    ArrayClass U_temp; 
    CallBLASdgemm(U, M, U_temp);   //Now U_temp contains the correct output.
    U = std::move(U_temp);
}

这是对 std::move 的有效使用,还是我以某种方式破坏了“复制省略”(或者由于某些其他原因它不好)?

编辑:请求 CallBLASDgemm 的签名;这是

CallBLASdgemm(const ArrayClass& U, const TransformMatrix* M, 
              ArrayClass& V);

最佳答案

在这种情况下,无论有没有复制省略,都不会执行任何复制,所以这已经不可能了。因为 U_temp 是一个左值,如果您这样做,编译器必须调用复制构造函数:

U = U_temp;

但是,您知道 U_temp 不会再被使用,因此移走它的值是完全安全的并且可能更快(也就是说,如果 ArrayClass 实现一个 move 赋值构造函数)。在这里使用 std::move 很好,甚至值得鼓励。

关于c++ - std::move 为 "in-place operation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572903/

相关文章:

c++ - 使用 FFmpeg 的音频均衡器

c++ - 模板偏特化

c++ - C++ 11多线程生产者/消费者程序挂起

c++ - std::move on std::string 是否保证 .c_str() 返回相同的结果?

c++ - 将 Vector 的第一个元素 move 到最后一个元素

c++ - 将 std::__cxx11::string 转换为 std::string

c++ - 用 C/C++ 编写日志文件

C++二叉树编程新节点离开范围问题

c++ - 如果模板参数无效,请在编译时检查

c# - System.IO.File.Delete()/System.IO.File.Move() 有时不起作用