[C++] Lvalue and Rvalue Reference

Lvalue and Rvalue Reference

int a = 10;// a is in stack

int& ra = a; // 左值引用

int* && pa = &a; // 右值引用,指针类型的引用

右值引用:用的是计算机CPU(寄存器)的值 或 内存的值。

左值引用:必须是内存的值。

时间: 2024-08-07 02:38:57

[C++] Lvalue and Rvalue Reference的相关文章

C++11标准之右值引用(rvalue reference)

1.右值引用引入的背景 临时对象的产生和拷贝所带来的效率折损,一直是C++所为人诟病的问题.但是C++标准允许编译器对于临时对象的产生具有完全的自由度,从而发展出了Copy Elision.RVO(包括NRVO)等编译器优化技术,它们可以防止某些情况下临时对象产生和拷贝.下面简单地介绍一下Copy Elision.RVO,对此不感兴趣的可以直接跳过: (1) Copy Elision Copy Elision技术是为了防止某些不必要的临时对象产生和拷贝,例如: struct A { A(int)

C++11新特性之 rvalue Reference(右值引用)

右值引用可以使我们区分表达式的左值和右值. C++11引入了右值引用的概念,使得我们把引用与右值进行绑定.使用两个"取地址符号": int&& rvalue_ref = 99; 需要注意的是,只有左值可以付给引用,如: int& ref = 9; 我们会得到这样的错误: "invalid initialization of non-const reference of type int& from an rvalue of type int&q

二十分钟弄懂C++11 的 rvalue reference (C++ 性能剖析 (5))

C++ 11加了许多新的功能.其中对C++性能和我们设计class的constructor或assignment可能产生重大影响的非rvalue reference莫属!我看了不少资料,能说清它的不多.下面我企图用简单的例子来说明,希望读者能够理解并应用这一重要的语言构造. 1.rvalue reference 是reference (即指针) 比如下面两条语句的语义完全一样: int &&p = 3;             // line 1 const int &cp = 3

L-value 和 R-value.

An L-value is something that can appear on the left side of an equal sign, An R-value is something that can appear on the right side of an equal sign. for example: 1 a = b + 25; 'a' is an L-value because it identifies a place where a result can be st

理解lvalue和rvalue

今天看C++模板的资料,里面说到lvalue,rvalue的问题,这个问题以前也看到过,也查过相关资料,但是没有考虑得很深,只知道rvalue不能取地址,不能赋值等等一些规则.今天则突然有了更深层次的理解(也可以说是顿悟,耗时不过几秒钟),记录下来. 下面是我对这两个单词字面的意思的猜测: lvalue估计来源于left value. 在赋值语句中lvalue = rvalue:位置处于左边.就是可以修改的值. rvalue估计来源于right value.处于赋值语句右边,是只读的不可修改的值

Google C++ Coding Style:右值引用(Rvalue Reference)

右值引用是一个C++11特性,标记为T&&.GSG中定义:只为移动建构函数(Move constructor)和移动赋值操作(Move assignment)使用右值引用.并且不要使用std::Forward(提供的完美转发特性). C++中右值指表达式结束时就不再存的临时对象.在C++11中,右值分为纯右值(即原始字面量,表达式产生的临时变量等),以及一个将亡值(expiring value, 使用<<深入应用C++11>>中的译法,指的是与右值引用相关的表达式,

C++11 thread(1)

原文地址:http://www.cplusplus.com/reference/thread/thread/thread/ public member function <thread> std::thread::thread default (1) thread() noexcept; initialization (2) template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&

【转载】关于Java String, StringBuilder, StringBuffer, Hashtable, HashMap的面试题

REF: http://blog.csdn.net/fightforyourdream/article/details/15333405 题目是一道简单的小程序,像下面这样:[java] view plaincopypublic class Test1 {   public static void main(String args[]) {    String s = new String("Hello");    System.out.println(s);      foo(s);

C++11中std::forward的使用 (转)

std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg is an lvalue reference, the function returns arg without modifying its type. std::forward:This is a helper function to allow perfect forwarding of arg