1 左值和右值
- 左值是指那些具有持久存储位置的表达式。右值是指那些没有持久存储位置的临时表达式,通常表示的是临时值或即将被销毁的对象。
- 简单理解,左值可以取地址,可以位于赋值运算符左侧。右值不可以取地址,通常在赋值运算符右侧。
- 示例
-
// data 可以处于赋值运算符左侧,也可以取地址,因此为左值 int data; data = 10010; &data; // 10 不能处于赋值运算符左侧,也不能取地址,因此为右值 10 = 2; // 编译不通过 &10; // 编译不通过
-
2 左值引用和右值引用
2.1 左值引用
- 左值引用就是指常说的引用,用
&声明,左值引用必须绑定到一个左值。 - 示例
-
int data = 10010; int &value1 = data; // data 为左值,可以绑定到value1上 int &value2 = 10; // 10为右值,不能绑定到value2上,编译会报错
-
- 但是可以将const类型的引用绑定到一个右值上,因为const类型的引用无法修改。
- 示例
-
const int &value = 10; // const类型引用可以绑定一个右值
-
2.2 右值引用
- 右值引用用
&&声明,是C++11增加的一个新的类型。右值引用必须绑定到一个右值。 - 示例
-
int data = 10010; int &&value1 = data; // data 为左值,不能绑定到value1上,编译会报错 int &&value2 = 10; // 10为右值,可以绑定到value2上
-
- 左值引用和右值引用本身都是左值
- 右值引用主要用于支持移动语义和完美转发
3 移动语义(std::move)
3.1 含义
- 上文介绍过,不能将一个左值绑定到右值引用上,但是使用
std::move方法可以将左值转换为右值,就可以实现将左值绑定到右值引用上。 - 示例
-
int data = 10010; int &&value1 = std::move(data); // 通过std::move将data转为右值,可以成功绑定到右值引用value1上
-
- 可以看下
std::move的源码-
template<class _Ty> _NODISCARD constexpr remove_reference_t<_Ty>&& move(_Ty&& _Arg) _NOEXCEPT { // forward _Arg as movable return (static_cast<remove_reference_t<_Ty>&&>(_Arg)); }
-
- 可以看到
std::move实际上是进行了一个强制转换,也就是把一个左值强制转换为右值。也就是说使用std::move这个函数并不能移动任何东西,而是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存拷贝。
3.2 作用
-
移动语义最常用的一个场景是实现移动拷贝构造和移动赋值运算符。
-
以
std::string为例,在进行拷贝构造或者赋值操作时,会进行深拷贝,这在一定程度上会影响程序性能,但是如果是使用移动拷贝构造和移动赋值运算符就不会发生深拷贝。 -
实现一个字符串类,看下具体应用。
-
#include <iostream> class MyString { private: char* data; // 存储字符串数据 size_t length; // 字符串长度 public: // 默认构造函数 MyString() : data(nullptr), length(0) { std::cout << "默认构造函数" << std::endl; } // 构造函数 MyString(const char* str) { std::cout << "构造函数" << std::endl; length = strlen(str); data = new char[length + 1]; memset(data, 0, length + 1); memcpy(data, str, length); } // 拷贝构造函数 MyString(const MyString& other) { std::cout << "拷贝构造函数" << std::endl; length = strlen(other.data); data = new char[length + 1]; memset(data, 0, length + 1); memcpy(data, other.data, length); } // 移动构造函数 MyString(MyString&& other) { std::cout << "移动构造函数" << std::endl; data = other.data; length = other.length; other.data = nullptr; other.length = 0; } // 拷贝赋值运算符 MyString& operator=(const MyString& other) { std::cout << "拷贝赋值运算符" << std::endl; if (this != &other) { delete[]data; length = strlen(other.data); data = new char[length + 1]; memset(data, 0, length + 1); memcpy(data, other.data, length); } return *this; } // 移动赋值运算符 MyString& operator=(MyString&& other) { std::cout << "移动赋值运算符" << std::endl; if (this != &other) { // 避免自赋值 delete[]data; data = other.data; length = other.length; other.data = nullptr; other.length = 0; } return *this; } void print() { if (data == nullptr) { std::cout << "length: " << length << std::endl; std::cout << "data is null" << std::endl; } else { std::cout << "length: " << length << ", data: " << data << std::endl; } } // 析构函数 ~MyString() { delete[]data; } }; int main() { { MyString str1("hello world"); MyString str2(std::move(str1)); MyString str3("hello world"); MyString str4; str4 = std::move(str3); str1.print(); str2.print(); str3.print(); str4.print(); } system("pause"); return 0; } -
打印结果
-
构造函数 移动构造函数 构造函数 默认构造函数 移动赋值运算符 length: 0 data is null length: 11, data: hello world length: 0 data is null length: 11, data: hello world 请按任意键继续. . .
-
-
拷贝构造就是实现深拷贝,而移动构造并未实现数据的拷贝,只是把当前数据指针项指向了传进来的对象的数据指针项。移动赋值同理。
-
也就是说,
str2对象的数据指针指向了str1对象的数据,后续str2使用的数据都是str1的数据,这样就实现了将str1的数据转移给了str2,后续由str2管理这部分数据。 -
那么在数据转移后,后续就不能再使用
str1对象了,在自定义实现时,需要将指针置为nullptr(否则会造成双析构),长度置为0。 -
C++的标准字符串的实现逻辑就是这样的,可以看下示例
-
#include <iostream> #include <string> int main() { { std::string str1("hello world"); std::string str2(std::move(str1)); std::string str3("hello world"); std::string str4; str4 = std::move(str3); std::cout << "str1 size: " << str1.size() << ", str1: " << str1 << std::endl; std::cout << "str2 size: " << str2.size() << ", str2: " << str2 << std::endl; std::cout << "str3 size: " << str3.size() << ", str3: " << str3 << std::endl; std::cout << "str4 size: " << str4.size() << ", str4: " << str4 << std::endl; } system("pause"); return 0; }
-
-
打印结果
-
str1 size: 0, str1: str2 size: 11, str2: hello world str3 size: 0, str3: str4 size: 11, str4: hello world 请按任意键继续. . .
-
-
C++ STL容器都是支持移动构造和移动赋值的
-
看下
std::vector-
#include <iostream> #include <string> #include <vector> int main() { { std::vector<int> v1; v1.push_back(1001); v1.push_back(1001); v1.push_back(1001); v1.push_back(1001); std::vector<int> v2(std::move(v1)); std::cout << "v1 size: " << v1.size() << std::endl; std::cout << "v2 size: " << v2.size() << std::endl; std::vector<int> v3; v3.push_back(1001); v3.push_back(1001); v3.push_back(1001); v3.push_back(1001); std::vector<int> v4; v4 = std::move(v3); std::cout << "v3 size: " << v3.size() << std::endl; std::cout << "v4 size: " << v4.size() << std::endl; } system("pause"); return 0; }
-
-
打印结果
-
v1 size: 0 v2 size: 4 v3 size: 0 v4 size: 4 请按任意键继续. . .
-
4 完美转发(std::forward)
- 完美转发用于在模板函数中将参数以原始类型(左值或右值)转发给其他函数。
- 先看这样一个示例
-
#include <iostream> #include <string> #include <vector> // 接收左值的函数 void process(int& value) { std::cout << "左值 value: " << value << "\n"; } // 接收右值的函数 void process(int&& value) { std::cout << "右值 value: " << value << "\n"; } // 包装函数,使用 std::forward 实现完美转发 template <typename T> void wrapper(T&& arg) { process(arg); } int main() { { int data = 10010; wrapper(data); // 传入左值, T被推导为左值引用 wrapper(256); // 传入右值, T被推导为右值引用 } system("pause"); return 0; }
-
- 打印结果
-
左值 value: 10010 左值 value: 256 请按任意键继续. . .
-
- 上文介绍过
&&表示右值引用,但是作为模板参数时,被称为万能引用或转发引用,而不是普通的右值引用。 - 传入
data时,期望的结果是调用接收左值的函数,传入256时,期望的结果是调用接收右值的函数。但实际情况下,传入左值和右值,调用的都是接收左值的函数。 - 分析下这种现象,首先传入左值data,T被推导为左值引用,是没有问题的。然后传入右值250,T被推导为右值引用,也是没有问题的。但是,上文介绍过,右值引用本身是一个左值,在调用
process接口时, 调用的自然就是接收左值的函数。 - 那么如何实现我们希望的场景呢?使用完美转发就可以解决。调用
process(arg);改为process(std::forward<T>(arg));。 - 再执行程序,看下结果
-
左值 value: 10010 右值 value: 256 请按任意键继续. . .
-
- 传入左值,调用接收左值的函数,传入右值,调用接收右值的函数。也就是
std::forward可以将参数以原始类型(左值或右值)转发给其他函数。
630




被折叠的 条评论
为什么被折叠?



