本文作者原创,首发在本博客;CSDN 上的同名文章已标注为转载并回链到本页:https://blog.csdn.net/qq_30220519/article/details/138619023
1 | void reference(int& v) { |
1 | int& fn(){...} //返回类型为左值引用 |
左值引用是用于常规对象的引用和修改
右值引用是为了优化资源管理,特别是临时对象的生命周期终结时能将其拥有的资源高效地转移给新的所有者。
常见用法1:
std::vector::emplace_back(T &&val)
常见用法2:
class A;
A oldVal;
A newVal = std::move(oldVal);
常见用法3:
class A {
public:
A(int&& oldV) : v(oldV) {}
A(const A& b){…} //copy construct
A(A&& b){…} //move construct
int v;
}
int&& v = 0;
int oldV = 0;
A oldVal(oldV);
A newVal(oldVal);
常见用法4:
template<typename… Args>
void h(Args&&… args)
{
reference(std::forward
}
常见用法5:
1 | class A final |
1 | auto &&delayA = func(); // 延长临时对象生命周期 |
常见用法6:
std::string a = “…..”;
std::string_view refRightA = a;
创作不易,小小的支持一下吧!

