一个基本概念:关于const 变量是否为左值

pony006 2015-11-03 12:41:36
const变量书上的观点有两派,一派为不可修改的左值,另一派为为右值。
到底谁是对的?
...全文
563 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-11
  • 打赏
  • 举报
回复
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!
fefe82 2015-11-10
  • 打赏
  • 举报
回复
引用 9 楼 lm_whales 的回复:
[quote=引用 7 楼 fefe82 的回复:] [quote=引用 6 楼 lm_whales 的回复:] [quote=引用 4 楼 fefe82 的回复:] A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
这句话可以这么理解么? 可修改左值,是个左值 不具有数组类型,不具有不完全类型,不具有常量性类型。 并且如果是个结构或者联合 没有任何 成员(包括,递归的,所包含的集合或者联合的 ,任何成员或者元素) 有常量性类型。 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 这段话里,好像有这么四层意思: 1)数组类型不是左值。 2)不完全类型不是左值 ,(不能定义对象?)。 3)常量性对象或者字面值,不是左值。 (只要用 const (,constexpr)定义,就不是左值?字面值也不是左值) 4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?) [/quote] 这句话解释的是 modifiable lvalue,他首先是一个 lvalue ,并且 1) 2) 3) 4) ... 这句话是在对 lvalue 进行分类。 1) 2) 3) 4) 都是 lvalue ,但不是 modifiable lvalue 。除了 1) 2) 3) 4) 之外的 lvalue ,都是 modifiable lvalue 。[/quote] 原来这么理解, 6.3.2.1 这段话里,除了modifiable lvalue 都没有突出说明 至少没有把 lvalue 分成modifiable lvalue 和非 modifiable lvalue 忽然出现一个modifiable lvalue 感觉和强调 lvalue就是modifiable lvalue 差不多[/quote] 呃 ... ,标准原文 modifiable lvalue 是加了斜体的,抱歉拷贝的时候没有调格式。
lm_whales 2015-11-10
  • 打赏
  • 举报
回复
引用 7 楼 fefe82 的回复:
[quote=引用 6 楼 lm_whales 的回复:] [quote=引用 4 楼 fefe82 的回复:] A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
这句话可以这么理解么? 可修改左值,是个左值 不具有数组类型,不具有不完全类型,不具有常量性类型。 并且如果是个结构或者联合 没有任何 成员(包括,递归的,所包含的集合或者联合的 ,任何成员或者元素) 有常量性类型。 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 这段话里,好像有这么四层意思: 1)数组类型不是左值。 2)不完全类型不是左值 ,(不能定义对象?)。 3)常量性对象或者字面值,不是左值。 (只要用 const (,constexpr)定义,就不是左值?字面值也不是左值) 4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?) [/quote] 这句话解释的是 modifiable lvalue,他首先是一个 lvalue ,并且 1) 2) 3) 4) ... 这句话是在对 lvalue 进行分类。 1) 2) 3) 4) 都是 lvalue ,但不是 modifiable lvalue 。除了 1) 2) 3) 4) 之外的 lvalue ,都是 modifiable lvalue 。[/quote] 原来这么理解, 6.3.2.1 这段话里,除了modifiable lvalue 都没有突出说明 至少没有把 lvalue 分成modifiable lvalue 和非 modifiable lvalue 忽然出现一个modifiable lvalue 感觉和强调 lvalue就是modifiable lvalue 差不多
paschen 2015-11-10
  • 打赏
  • 举报
回复
const 其实是编译器判断实现的常量,是一个假常量,在实际中,使用const定义一个常量,最终还是一个变量,只是在编译器内进行了检查,发现有修改则报错。出于这种原因,const其实是可修改的(全局const变量则不能修改,因为是在常量区的)。 当然,如果const变量的值是在编译时就可确定的,如const int a = 5; 同时代码中没有显式获取该变量的地址,那么编译器也会将其优化成一个真常量
fefe82 2015-11-10
  • 打赏
  • 举报
回复
引用 6 楼 lm_whales 的回复:
[quote=引用 4 楼 fefe82 的回复:] A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
这句话可以这么理解么? 可修改左值,是个左值 不具有数组类型,不具有不完全类型,不具有常量性类型。 并且如果是个结构或者联合 没有任何 成员(包括,递归的,所包含的集合或者联合的 ,任何成员或者元素) 有常量性类型。 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 这段话里,好像有这么四层意思: 1)数组类型不是左值。 2)不完全类型不是左值 ,(不能定义对象?)。 3)常量性对象或者字面值,不是左值。 (只要用 const (,constexpr)定义,就不是左值?字面值也不是左值) 4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?) [/quote] 这句话解释的是 modifiable lvalue,他首先是一个 lvalue ,并且 1) 2) 3) 4) ... 这句话是在对 lvalue 进行分类。 1) 2) 3) 4) 都是 lvalue ,但不是 modifiable lvalue 。除了 1) 2) 3) 4) 之外的 lvalue ,都是 modifiable lvalue 。
lm_whales 2015-11-10
  • 打赏
  • 举报
回复
引用 4 楼 fefe82 的回复:
A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type.
这句话可以这么理解么? 可修改左值,是个左值 不具有数组类型,不具有不完全类型,不具有常量性类型。 并且如果是个结构或者联合 没有任何 成员(包括,递归的,所包含的集合或者联合的 ,任何成员或者元素) 有常量性类型。 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 这段话里,好像有这么四层意思: 1)数组类型不是左值。 2)不完全类型不是左值 ,(不能定义对象?)。 3)常量性对象或者字面值,不是左值。 (只要用 const (,constexpr)定义,就不是左值?字面值也不是左值) 4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?)
张小飞Official 2015-11-03
  • 打赏
  • 举报
回复
c++11 const是分左值跟右值的,楼上说的好这书太老了,可以扔了
fefe82 2015-11-03
  • 打赏
  • 举报
回复
c11 draft n1570 6.3.2 Other operands 6.3.2.1 Lvalues, arrays, and function designators 1 An lvalue is an expression (with an object type other than void) that potentially designates an object; if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a constqualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a constqualified type. c++14 draft n4296 3.10 Lvalues and rvalues [basic.lval] 1 Expressions are categorized according to the taxonomy in Figure 1. (1.1) — An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue. — end example ] (1.2) — An xvalue (an “eXpiring” value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). Certain kinds of expressions involving rvalue references (8.3.2) yield xvalues. [ Example: The result of calling a function whose return type is an rvalue reference to an object type is an xvalue (5.2.2). — end example ] (1.3) — A glvalue (“generalized” lvalue) is an lvalue or an xvalue. (1.4) — An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assignment expression) is an xvalue, a temporary object (12.2) or subobject thereof, or a value that is not associated with an object. (1.5) — A prvalue (“pure” rvalue) is an rvalue that is not an xvalue. [ Example: The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is also a prvalue. — end example ] Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category. [ Note: The discussion of each built-in operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it expects. For example, the built-in assignment operators expect that the left operand is an lvalue and that the right operand is a prvalue and yield an lvalue as the result. User-defined operators are functions, and the categories of values they expect and yield are determined by their parameter and return types. — end note ]
sdghchj 2015-11-03
  • 打赏
  • 举报
回复
右值是不能对其取地址的量,一般是临时变量、匿名对象、字面常量。 左值就是能对其取地址的量。 const确实与左右值无关,但一般能被const修饰的肯定只能是左值,也有例外,如const函数返回值,就是const了一个临时变量。
fly_dragon_fly 2015-11-03
  • 打赏
  • 举报
回复
楼上说得对, 这书可以扔了
ri_aje 2015-11-03
  • 打赏
  • 举报
回复
const 和左右值没关系。既有左值的 const 变量,也有右值的。
引用 楼主 pony006 的回复:
const变量书上的观点有两派,一派为不可修改的左值,另一派为为右值。
这书还是别看了,省得越学越差。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧