当前位置:首页 » 《关于电脑》 » 正文

【C++】std::move用法详解 - 移动语义的艺术

21 人参与  2024年05月25日 19:12  分类 : 《关于电脑》  评论

点击全文阅读


由于在工作中经常需要使用std::move,但一直没有总结过用法,有种一知半解的感觉,所以今天抽空总结一下。

std::move 是 C++11 引入的一个函数模板,位于 <utility> 头文件中。它用于将对象的所有权从一个对象转移到另一个对象,通常与右值引用一起使用,实现移动语义。

使用场景

避免不必要的拷贝: 当有一个临时对象,且不再需要这个临时对象的内容时,使用 std::move 可以避免不必要的拷贝,提高性能。

std::string createString() {    std::string result = "Some string";    return result;  // 返回值会被移动}int main() {    std::string target = createString();  // 使用 std::move 避免复制    // ...}

移动语义: 在实现移动构造函数和移动赋值运算符时,使用 std::move 可以将对象的内容从一个对象移动到另一个对象,而不是进行深拷贝。

class MyString {public:    // 移动构造函数    MyString(MyString&& other) noexcept {        // 使用 std::move 将资源从 other 移动到 this        data_ = std::move(other.data_);        size_ = other.size_;        other.size_ = 0;    }    // 移动赋值运算符    MyString& operator=(MyString&& other) noexcept {        // 使用 std::move 将资源从 other 移动到 this        if (this != &other) {            data_ = std::move(other.data_);            size_ = other.size_;            other.size_ = 0;        }        return *this;    }private:    std::string data_;    size_t size_;};

工作原理

std::move 实际上是一个类型转换函数,将给定的左值强制转换为右值引用。这样做的目的是为了通知编译器,在这个右值引用上调用的函数可以获取其内容的所有权,从而触发移动语义。

template <typename T>typename std::remove_reference<T>::type&& move(T&& arg) noexcept {    return static_cast<typename std::remove_reference<T>::type&&>(arg);}

最佳实践

仅在必要时使用: 不要滥用 std::move。只有在确实需要移动语义时才使用,过度使用可能导致代码不必要地复杂化。

不要对可能会被再次使用的对象使用 std::move: 如果之后还需要使用对象的值,不要使用 std::move,因为它会使对象进入“移后即掷”的状态。

使用在移动语义上有定义的类: 当设计自己的类时,确保正确实现了移动构造函数和移动赋值运算符。

在性能敏感的代码中使用: 在对性能要求较高的代码路径中,使用 std::move 可以显著提高效率。

使用 std::forward 更通用的情况: 在泛型编程中,使用 std::forward 通常比直接使用 std::move 更通用,因为它可以正确处理左值引用和右值引用。

template <typename T>void process(T&& arg) {    // 使用 std::forward,可以正确处理左值引用和右值引用    some_function(std::forward<T>(arg));}

std 标准库中的类型需要用move吗

std:: 标准库中的类型通常已经正确实现了移动语义,因此在大多数情况下,不需要显式使用 std::move 来获得性能提升。标准库中的类型已经针对移动语义进行了优化,它们在移动构造函数和移动赋值运算符中已经使用了合适的操作。

例如,std::vectorstd::string 等容器和字符串类型已经支持移动语义,因此当对它们进行赋值或传递时,编译器会自动使用移动语义。下面是一个使用 std::move 的例子:

#include <vector>#include <iostream>int main() {    std::vector<int> source = {1, 2, 3, 4, 5};    // 使用 std::move 将 source 的内容移动到 target    std::vector<int> target = std::move(source);    // source 现在可能处于有效但未定义的状态    // 输出 target 的内容    for (int value : target) {        std::cout << value << " ";    }    return 0;}

在这个例子中,std::move 用于将 source 的内容移动到 target 中。然而,对于 std::vector 这样的标准库容器,实际上直接使用赋值运算符也是可以的:

std::vector<int> target = source;  // 编译器会自动使用移动语义

总体而言,在使用标准库的情况下,不需要过度使用 std::move。编译器会根据上下文自动选择最合适的操作,因此可以编写简洁而清晰的代码而不必手动添加 std::move

后记

就我个人而言,我觉得用到std::move最多的地方是定义的一个结构体作为另一个结构体初始化时的成员变量,此时一般要用std::move避免不必要的拷贝,而std标准库下的类型例如std::vector一般就直接赋值,不用std::move,因为std:: 标准库中的类型通常已经正确实现了移动语义。

例如:

PrimitiveStructure::PrimitiveStructure(    SpeedInfoPrimitive&& speed_info_primitive_in,    HeuristicPrimitive&& heuristic_primitive_in,    : speed_info_primitive(std::move(speed_info_primitive_in)),      heuristic_primitive(std::move(heuristic_primitive_in))) {}

注意,应用std::move的变量不能是const常量,否则编译会报错。


点击全文阅读


本文链接:http://zhangshiyu.com/post/113587.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1