目录
左值与右值的定义
左值:表达式结束时,依然存在的持久对象;(左值的地址可以获取);
int main()
{
//p *p b c皆为左值
int *p = new int(0);
cout << &p << endl;
cout << &(*p) << endl;
int b = 1;
cout << &b << endl;
const int c = 2;
cout << &c << endl;
return 0;
}
运行结果:
//func1()函数的返回值为左值
int& func1()
{
static int x = 0;
return x;
}
int main()
{
cout << &func1() << endl;
return 0;
}
运行结果:
static修饰局部变量,延长了局部变量的生命周期,局部变量出作用域不会被销毁,采用传引用返回,由于共用同一块内存空间,没有拷贝的需求,则此过程中也就不会产生临时对象,所以func1()函数的返回值为左值;
右值:表达式结束时,不再存在的临时对象;(右值的地址不可以获取);
int main()
{
double x = 1.1, y = 2.2;
//字面量的生命周期仅限于它所在的表达式,所以字面量可以被视为临时对象
10;
//cout << &(10) << endl;(×)
//生成临时的double类型的对象存储表达式x+y计算所得出的结果
x + y;
//cout << &(x + y) << endl;(×)
return 0;
}
//func2()函数的返回值为右值
int func2()
{
static int x = 0;
return x;
}
int main()
{
cout << &func2() << endl;
return 0;
}
static修饰局部变量,延长了局部变量的生命周期,局部变量出作用域不会被销毁,采用传值返回,此过程会生成临时对象,首先将返回值x拷贝到临时对象,然后将临时对象的作为func2()函数真正的返回值,所以func2()函数的返回值为右值;
左值引用与右值引用
左值引用是给左值取别名,不开辟内存空间,通过"&"声明为左值引用;
int main()
{
// 以下的p、b、c、*p都是左值
int* p = new int(0);
int b = 1;
const int c = 2;
// 以下几个是对上面左值的左值引用
int*& rp = p;
int& rb = b;
const int& rc = c;
int& pvalue = *p;
return 0;
}
右值引用是给右值取别名,不开辟内存空间,通过"&&"声明为右值引用;
int main()
{
double x = 1.1, y = 2.2;
// 以下几个都是常见的右值
10;
x + y;
fmin(x, y);
// 以下几个都是对右值的右值引用
int&& rr1 = 10;
double&& rr2 = x + y;
double&& rr3 = fmin(x, y);
return 0;
}
左值引用与右值引用的对比
左值引用总结
1. 左值引用只能引用左值,不能引用右值;
2. 但是const左值引用既可引用左值,也可引用右值;
int main()
{
// 左值引用只能引用左值,不能引用右值
int a = 10;
int& ra1 = a;// ra1为a的别名
//int& ra2 = 10; // 编译失败,因为10是右值
// const左值引用既可引用左值,也可引用右值
const int& ra3 = 10;
const int& ra4 = a;
return 0;
}
右值引用总结
1. 右值引用只能右值,不能引用左值;
2. 但是右值引用可以move以后的左值;
int main()
{
// 右值引用只能右值,不能引用左值。
int&& r1 = 10;
// error C2440: “初始化”: 无法从“int”转换为“int &&”
// message : 无法将左值绑定到右值引用
int a = 10;
int&& r2 = a;
// 右值引用可以引用move以后的左值
int&& r3 = std::move(a);
return 0;
}
右值引用的使用场景
左值引用的意义:
- 函数传参:实参传给形参时,传引用可以减少拷贝;
- 函数传返回值时,只要是出作用域仍然存在的对象,那么传引用返回可以避免拷贝; 但是左值引用并没有彻底的解决问题:函数传返回值时,如果返回值是出作用域销毁的对象,只能传值返回,则需要多次的拷贝构造,导致消耗较大,效率降低;
右值引用间接地解决了函数传返回值(返回对象已经销毁)时的多次拷贝问题;
场景一
namespace RightReference
{
class string
{
public:
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string(const char* str = "")
:_size(strlen(str))
, _capacity(_size)
{
//cout << "string(char* str)" << endl;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
// 拷贝构造
string(const string& s)
:_str(nullptr)
{
cout << "string(const string& s) --- 深拷贝" << endl;
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
// 拷贝赋值
string& operator=(const string& s)
{
cout << "string& operator=(const string& s) --- 深拷贝" << endl;
string tmp(s);
swap(tmp);
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch)
{
if (_size >= _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
const char* c_str() const
{
return _str;
}
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
};
RightReference::string to_string(int value)
{
bool flag = true;
if (value < 0)
{
flag = false;
value = 0 - value;
}
RightReference::string str;
while (value > 0)
{
int x = value % 10;
value /= 10;
str += ('0' + x);
}
if (flag == false)
{
str += '-';
}
std::reverse(str.begin(), str.end());
return str;
}
}
int main()
{
RightReference::string ret1 = RightReference::to_string(-1234);
}
当使用to_string()函数的返回值来构造ret对象,涉及到函数传值返回时的拷贝问题:
移动构造
当使用to_string()函数的返回值来构造ret对象,中间会生成临时对象,临时对象的属性为右值,若实现右值引用版本的拷贝构造函数,根据编译器优先匹配的原则,右值一定调用右值引用版本的拷贝构造函数;
// 类中实现移动构造(右值引用版本的拷贝构造函数)
// 拷贝构造--左值
//(const左值引用既可引用左值,又可以引用右值)
string(const string& s)
:_str(nullptr)
{
cout << "string(const string& s) --- 深拷贝" << endl;
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
//移动构造--右值
string(string&& s)
{
cout << "string(string&& s) -- 移动拷贝" << endl;
swap(s);
}
int main()
{
RightReference::string s1("abcdef");
RightReference::string s2(s1);
RightReference::string s3(std::move(s1));
return 0;
}
运行结果:
右值分为纯右值与将亡值,纯右值指的是内置类型的右值,将亡值指的是自定义类型的右值,当使用已存在的对象且对象的属性为左值去构造另一个不存在的对象时,则调用拷贝构造初始化不存在的对象;当使用已存在的对象且对象的属性为右值去构造另一个不存在的对象时,则调用移动构造初始化不存在的对象;
移动构造的本质是将右值的资源直接转移,占为己有,不用做深拷贝,窃取别人的资源来构造自己,移动构造并没有开辟空间,拷贝数据,所以效率提升;
移动赋值
int main()
{
RightReference::string ret1;//调用默认构造函数初始化ret1对象
ret1= RightReference::to_string(-1234);
}
运行结果:
当使用to_string()函数的返回值来构造ret对象,中间会生成临时对象,临时对象的属性为右值,若实现右值引用版本的赋值运算符重载函数,根据编译器优先匹配的原则,右值一定调用右值引用版本的赋值运算符重载函数;
//赋值运算符重载(拷贝赋值)
string& operator=(const string& s)
{
cout << "string& operator=(const string& s) --- 深拷贝" << endl;
string tmp(s);//调用拷贝构造
swap(tmp);
return *this;
}
// 移动赋值(右值引用版本赋值运算符重载函数)
string& operator=(string&& s)
{
cout << "string& operator=(string&& s) -- 移动拷贝" << endl;
swap(s);
return *this;
}
string的赋值运算符重载函数(拷贝赋值)的形参为const左值引用,所以既可以接收右值,又可以接收左值,但是拷贝赋值中调用了拷贝构造函数进行了深拷贝,而移动赋值函数只需要调用swap函数进行资源转移,因此调用移动赋值的代价远小于调用拷贝赋值;
需要注意的是使用一个已经存在的对象接收to_string的返回值时,中间一定会生成临时对象,编译器无法优化,若临时对象比较小,存放于寄存器;若临时对象比较大,则存放于to_string()与main()函数的函数栈帧之间;
场景二
容器的插入接口
int main()
{
list<RightReference::string> lt;
RightReference::string s1("1111");
// 调用拷贝构造
lt.push_back(s1);
// 调用移动构造
lt.push_back("2222");
lt.push_back(std::move(s1));
return 0;
}
运行结果:
完美转发
int main()
{
int&& r = 10;//10->右值
r++;//r->右值引用
cout << r << endl;
return 0;
}
运行结果:
右值被右值引用之后,右值引用本身的属性为左值(右值引用本身可以被修改);
void Fun(int& x)
{
cout << "左值引用" << endl;
}
void Fun(const int& x)
{
cout << "const 左值引用" << endl;
}
void Fun(int&& x)
{
cout << "右值引用" << endl;
}
void Fun(const int&& x)
{
cout << "const 右值引用" << endl;
}
// 模板中的&&不代表右值引用,而是万能引用,其既能接收左值又能接收右值。
// 模板的万能引用只是提供了能够接收同时接收左值引用和右值引用的能力,
// 但是引用类型的唯一作用就是限制了接收的类型,后续使用中都退化成了左值,
// 我们希望能够在传递过程中保持它的左值或者右值的属性, 就需要使用完美转发
template<typename T>
void PerfectForward(T&& t)
{
Fun(t);
}
int main()
{
PerfectForward(10); // 右值
int a;
PerfectForward(a); // 左值
PerfectForward(std::move(a)); // 右值
const int b = 8;
PerfectForward(b); // const 左值
PerfectForward(std::move(b)); // const 右值
return 0;
}
运行结果:
std::forward完美转发在传参过程中保留对象原生类型的属性
void Fun(int& x)
{
cout << "左值引用" << endl;
}
void Fun(const int& x)
{
cout << "const 左值引用" << endl;
}
void Fun(int&& x)
{
cout << "右值引用" << endl;
}
void Fun(const int&& x)
{
cout << "const 右值引用" << endl;
}
template<typename T>
void PerfectForward(T&& t)
{
//保持对象原生类型属性
Fun(forward<T>(t));
}
int main()
{
PerfectForward(10); // 右值
int a;
PerfectForward(a); // 左值
PerfectForward(std::move(a)); // 右值
const int b = 8;
PerfectForward(b); // const 左值
PerfectForward(std::move(b)); // const 右值
return 0;
}
运行结果:
欢迎大家批评指正,博主会持续输出优质内容,谢谢各位观众老爷观看,码字画图不易,希望大家给个一键三连支持~ 你的支持是我创作的不竭动力~