1 Star 2 Fork 0

岑剑伟 / CPlusPlusThings

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rvalue.cpp 2.44 KB
一键复制 编辑 原始数据 按行查看 历史
//
// Created by light on 19-11-4.
//
#include <iostream>
#include <complex>
#include <vector>
using namespace std;
// Rvalue references 解决非必要的拷贝 当赋值右手边是一个"右值",左手边对象可以偷右手边对象,而不需要重新分配内存。
// 浅copy
int foo() { return 5; }
class Foo {
public:
Foo() = default;
Foo(const Foo &foo) = default;
Foo(Foo &&foo) noexcept {}
};
// 判断调用哪一个函数
void process(int &i) {
cout << "左值传入" << endl;
}
void process(int &&i) {
cout << "右值传入" << endl;
}
void UnPerfectForward(int &&i) {
cout << "forward(int&& i)" << endl;
process(i);
}
// std::forward()实现就是下面这样
void PerfectForward(int &&i) {
cout << "forward(int&& i)" << endl;
process(static_cast<int&&>(i));
}
// Lvalue: 变量
// Rvalue: 临时对象就是个右值,右值不可以放在左边
int main() {
int a = 9, b = 4;
a = b;
b = a;
// a+b=42;// error Rvalue
string s1("hello");
string s2("world");
s1 + s2 = s2; // ok
string() = "ok"; // ok
cout << "s2:" << s1 + s2 << endl;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
complex<int> c1(3, 8), c2(1, 0);
c1 + c2 = complex<int>(3, 4); // ok
complex<int>() = complex<int>(1, 2); // ok
int x = foo();
// int *p=&foo(); //error! Rvalue不可以取地址
// foo()=7; // error
// Rvalue references
// vector<Foo> vec;
// vec.insert(vec.begin(), Foo()); // Rvalue references and Move Semantics
// 原先是下面这个
// iterator insert(const_iterator __position, const value_type& __x);
// 调用下面这个Move Semantics
// iterator insert(const_iterator __position, value_type&& __x) // 然后转交给Foo(Foo&& foo)
// { return emplace(__position, std::move(__x)); }
// Foo()这个临时对象为右值交给insert的move assignment然后再交给Foo的move ctor。中间有可能会丢失一些信息。
int aa = 1;
process(aa); // L
process(1); // R
process(move(aa)); // R
UnPerfectForward(2); // 希望通过转交调用的是右值传入函数,可是调用的是左值传入 这就是个Unperfect Forwarding
UnPerfectForward(move(aa)); // 同上
// 那如何设计Perfect Forwarding?
// 为传递加上static_cast<int &&>强转或者直接使用std::forward()
PerfectForward(2);
PerfectForward(move(aa));
return 0;
}
C++
1
https://gitee.com/genvex/CPlusPlusThings.git
git@gitee.com:genvex/CPlusPlusThings.git
genvex
CPlusPlusThings
CPlusPlusThings
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891