这可能与其他问题非常相似;我环顾四周,但我不太清楚自己在说什么。
我正在编写一个“应该”就地的函数,但它是由 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/