Value Categories
Value categories
Three primary categories
Each C++ expression (an operator with its operands, a literal, a variable name, etc.) is characterized by two independent properties: a type and a value category. Each expression has some non-reference type, and each expression belongs to exactly one of the three primary value categories.
每一个C++表达式有两个特征:类型和值分类
Primary categories
The primary value categories correspond to two properties of expression:
- has identity- 标识符:it's possible to determine whether the expression refers to the same entity as another expression, such as by comparing addresses of the objects or the functions they identify(obtained directly or indirectly);
- can be moved from:move constructor,move assignment operator,or another overload function that implements move semantics can bind to the expression.
move constructor:A move constructor of class T is a non-template
constructor whose first parameter is T&&, const T&&, volatile T&&, or const volatile T&&, and either there are no other parameters, or the rest of the parameters all have default values.
A move assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T&&, const T&&, volatile T&&, or const volatile T&&.
Expressions that:
- have identity and cannot be moved from are called lvalue expressions;
- have identity and can be moved from are called xvalue expressions;
- do not have identity and can be moved from area called prvalue expression;(比如常量等)
- do not have identity and cannot be moved from are not used.
lvalue
An lvalue("left value") expression is an expression that has identity and cannot be moved from.
The naming is historic and reflects the use of lvalue expressions as the left-hand operand of the assignment operator in the CPL programming language.
The following expressions are lvalue expression:
- the name of a variable or a function in scope, regardless of type, such as
std::cinorstd::endl. Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression; a = b, a += b, a %= b, and all other built-in assignment and compound assignment expressions++a and --a, the built-in pre-increment and pre-decrement expressions;*p, the built-in indirection expressiona[n] and p[n], the built-in subscript expressiona.m, the member of object expression, except where m is a member enumerator or a non-static member function, or where a is an rvalue and m is a non-static data member of non-reference type;p->m, the built-in member of pointer expression, except where m is a member enumerator or a non-static member function;a.*mp, the pointer to member of object expression, where a is an lvalue and mp is a pointer to data member;- p->*mp, the built-in pointer to member of pointer expression, where mp is a pointer to data member;
a, b, the built-in comma expression, where b is an lvalue;a ? b : c, the ternary conditional expression for some a, b, and c;- a
string literal, such as "Hello, world!"; - a cast expression to lvalue reference type, such as
static_cast<int&>(x);
Properties:
- Same as glvalue
- Address of lvalue may be taken: &++i and
std::endlare valid expressions. - A modifiable lvalue may be used as the left-hand operand of the built-in assignment and compound assignment operators.
- An lvalue may be used to initialize an lvalue reference; this associates a new name with the object identified by the expression.
rvalue (until C++11) prvalue (since C++11)>
A prvalue ("pure rvalue") expression is an expression that does not have identity and can be moved from.
The following expressions are prvalue expression:
- a literal (except for string literal), such as 42,
trueornullptr; - a function call or an overloaded operator expression of non-reference return type, such as
str.substr(1, 2),str1 + str2, orit++; a++ and a--, the built-in post-increment and post-decrement expressions;a + b, a % b, a & b, a << b, and all other built-in arithmetic expressions;a && b, a || b, ~a, the built-in logical expressions;a < b, a == b, a >= b, and all other built-in comparison expressions;&a, the built-in address-of expression;a.m, the member of object expression, where m is a member enumerator or a non-static member function[3], or where a is an rvalue and m is a non-static data member of non-reference type (until C++11);p->m, the built-in member of pointer expression, where m is a member enumerator or a non-static member function- a.*mp, the pointer to member of object expression, where mp is a pointer to member function[3], or where a is an rvalue and mp is a pointer to data member (until C++11)
p->*mp, the built-in pointer to member of pointer expression, where mp is a pointer to member functiona, b, the built-in comma expression, where b is an rvalue;a ? b : c, the ternary conditional expression for some a, b, and c- a cast expression to non-reference type, such as
static_cast<double>(x), std::string{}, or (int)42
Properties:
- Same as rvalue
- A prvalue cannot be polymorphic: the dynamic type of the object it identifies is always the type of the expression.
- A non-class prvalue cannot be cv-qualified.
xvalue
An xvalue -("expiring value") expression is an expression that has identity and can be moved from.
The following expressions are xvalue expressions:
- a function call or an overloaded operator expression of rvalue reference to object return type, such as
std::move(x);
Properties
- Same as rvalue
- Same as glvalue
Mixed categories
glvalue
A glvalue ("generalized lvalue") expression is an expression that is either an lvalue or an xvalue. It has identity. It may or may not be moved from.
Properties:
- a glvalue may be implicitly converted to a prvalue with lvalue-to-rvalue,array-to-pointer,function-to-pointer implicit convention.
- A glvalue may be polymorphic: the dynamic type of the object it identifies is not necessarily the static type of the expression.
- A glvalue can have incomplete type, where permitted by the expression.
rvalue
An rvalue("rigthj value") expression is an expression that is either prvalue or an xvalue. It can be moved from. It may or may not have identity.
The naming is historic and reflects the use of rvalue expressions as the right-hand operand of the assignment operator in the CPL programming language.
Properties :
- Address of an rvalue may not be taken:
&int(), &i++, &42, and &std::move(x)are invalid. - An rvalue can't be used as the left-hand operand of the built-in assignment or compound assignment operators.
- An rvalue may be used to initialize a const lvalue reference, in which case the lifetime of the object identified by the rvalue is extended until the scope of the reference ends.
Special categories
Pending member function call
The expressions a.mf and p->mf, where mf is a non-static member function, and the expressions a.*mfp and p->*mfp, where mfp is a pointer to member function, are classified as prvalue expressions, but they cannot be used to initialize references, as function arguments, or for any purpose at all, except as the left-hand argument of the function call operator, e.g. (p->*mfp)(args).
Void expressions
Function call expressions returning void, cast expressions to void, and throw-expressions are classified as prvalue expressions, but they cannot be used to initialize references or as function arguments.
Bit fields
An expression that designates a bit field (e.g. a.m, where a is an lvalue of type struct A { int m: 3; }) is an lvalue expression:
it may be used as the left-hand operand of the assignment operator, but its address cannot be taken and a non-const lvalue reference cannot be bound to it.
A const lvalue reference can be initialized from a bit-field lvalue, but a temporary copy of the bit-field will be made: it won't bind to the bit field directly.(不会直接绑定到原来值上的,而是一个临时的副本)
Value Categories的更多相关文章
- iOS开发中可能有用的那些分类们Categories
Categories是给你得不到源码的classes增加功能的一种方法. UIImageView+FaceAwareFill 这个类别使用了Aspect Fill内容模式,可以自动根据图像内容进行调整 ...
- Questions that are independent of programming language. These questions are typically more abstract than other categories.
Questions that are independent of programming language. These questions are typically more abstract ...
- Objective-C categories in static library
ASK: Can you guide me how to properly link static library to iphone project. I use staic library pro ...
- instance variables may not be placed in categories
Avoid Properties in Categories Objective-C分类中是不允许增加成员变量的(Instance variables may not be placed in cat ...
- FusionCharts封装-dataset和categories
Chart.java: /** * @Title:Chart.java * @Package:com.fusionchart.model * @Description:FusionCharts 封装d ...
- opencart3调用三级菜单level 3 sub categories
Opencart 3的menu菜单默认只调用一级和二级菜单,但很多电商网站类目复杂,三级菜单一般都是需要的,甚至更深,那么如何调用三级菜单level 3 sub categories呢?ytkah有一 ...
- Categories VS Extensions (分类 vs 扩展)
转载翻译自:http://rypress.com/tutorials/objective-c/categories 一.Categories(分类) Categories是一个把单个类定义分 ...
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
Objective-C RegEx Categories https://github.com/bendytree/Objective-C-RegEx-Categories 使用说明:将 RegExC ...
- Capterra Software Categories
https://www.capterra.com/categories this software categories is valuable.
随机推荐
- 使用Fiddler解析WCF RIA Service传输的数据
原文 http://www.cnblogs.com/wintersun/archive/2011/01/05/1926386.html 使用Fiddler 2 解析WCF RIA Service传输的 ...
- iOS中菊花。。。
其实系统的菊花除了功能有点单一,不具有刷新页面的功能以外,其他都还好吧,满足你一些正常的提示功能还是绰绰有余的:直接把项目里面的代码粘出来吧,其实也没有什么特别要注意的地方,一些设置属性也算留个备份 ...
- c++11新特性(4) lambda捕捉块
lambda表达式中的方括号成为捕捉块,能够在这里指定怎样从所在的作用域中捕捉变量. 捕捉的意思是指能够在该lambda中使用该变量.即能够捕获外部变量在lambda表达式内使用. 能够使用两种方式来 ...
- 实习生的Django[1]
尽管学期尚未结束,暑假尚未到来,可是大三的同学非常多已经和我一样開始实习或者实习一段时间了.我仅仅面试了一间数据挖掘的公司的研发部,还算顺利通过. 来这里实习后,由于网络原因,昨天没有刷题也没有写BL ...
- 在 win 10 中使用sql 2012 附加低版本数据失败的解决办法。
随着win 10 的发布,我也尝试把自己的笔记本升级下,体验win10,由于自己电脑好长时间没有管理过,东西比较乱,一激动就格式了硬盘.但是所有的资料都丢失了,不过我都提前备份到网盘上.好了,废话不多 ...
- 关于js封装框架类库之属性操作
在对DOM对象操作时,往往都要涉及到其属性的操作,为了提高开发效率,同时兼顾浏览器的性能,在这简单的封装了几个常见的属性.因为是模块化,在这只是引入了部分代码,其他代码在前几篇模块封装中有写.如有不足 ...
- Ajax异步请求XMLHttpRequest对象Get请求
$(function () { $("#btnGetDate").click(function () { var xhr; //第一步:创建异步请求的核心的对象: if (XMLH ...
- DevExpress ASP.NET 使用经验谈(5)-通过ASPxGridView实现CRUD操作
这节,我们将通过使用DevExpress的ASPxGridView控件,实现对数据的CRUD操作. 首先,我们在解决方案中,添加一个网站: 图一 添加新网站 图二 添加DevExpress.Data. ...
- ViewState是什么
在做ASP.NET的时候遇到ViewState,当时不知道他是什么意思. 就在当前页面中保存数据的. 像session.是会话级别的.只要会话没有过期.session中存的数据就在. viewstat ...
- MSSQL:修改tempdb设置增加DW性能
Temp DB 在DW中变得非常重要,因为要进行大量的运算,如果内存不够数据就会放在Temp DB中 1. 把Temp DB移动到高性能的磁盘上. 2. 增加tempdb 的大小 3. 把Auto S ...