c++11右值引用

本文深入解析C++11中左值、纯右值及将亡值的概念,详细阐述了std::forward与std::move函数的使用与区别,通过实例演示如何正确使用右值引用提升代码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.  参考:c++11之左值、纯右值、将亡值   https://blog.youkuaiyun.com/StephenZou14/article/details/77817114

 

 

2. 

std::forword()原型, 头文件<utility>

lvalue (1)
template <class T> T&& forward (typename remove_reference<T>::type& arg) noexcept;
rvalue (2)
template <class T> T&& forward (typename remove_reference<T>::type&& arg) noexcept;

 

If arg is an lvalue reference, the function returns arg with its type unchanged.
Otherwise, the function returns an rvalue reference (T&&) that refers to arg that can be used to pass an rvalue.

 

std::move()原型,头文件<utility>

template <class T> typename remove_reference<T>::type&& move (T&& arg) noexcept;

Return an rvalue reference that refers to arg.

 

3. 

pure rvalue 就是计算的中间结果,或字面量(处理字符串字面量),即C++11以前的右值。

expiring value 就是表达式返回的右值引用,如std::move()返回的值,或其他返回T&&的函数的返回值。

 

#include <type_traits> // std::is_lvalue_reference std::is_rvalue_reference


    int i[10] = {0,1,2,3,4,5,6,7,8,9};
    cout << boolalpha;
    cout << is_lvalue_reference<decltype(i[100])>::value << endl;
    cout << is_rvalue_reference<decltype(i[100])>::value << endl;
    cout << is_lvalue_reference<decltype("abc")>::value << endl;
    cout << is_rvalue_reference<decltype("abc")>::value << endl;

 

4.  参考 

https://en.cppreference.com/w/cpp/utility/forward

https://en.cppreference.com/w/cpp/language/value_category

http://www.stroustrup.com/terminology.pdf

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值