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

[复制链接]
查看11 | 回复10 | 2021-1-27 06:45:35 | 显示全部楼层 |阅读模式
const变量书上的观点有两派,一派为不可修改的左值,另一派为为右值。
到底谁是对的?
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
const和左右值没关系。既有左值的const变量,也有右值的。
引用楼主pony006的回复:const变量书上的观点有两派,一派为不可修改的左值,另一派为为右值。
这书还是别看了,省得越学越差。
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
楼上说得对,这书可以扔了
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
右值是不能对其取地址的量,一般是临时变量、匿名对象、字面常量。
左值就是能对其取地址的量。
const确实与左右值无关,但一般能被const修饰的肯定只能是左值,也有例外,如const函数返回值,就是const了一个临时变量。
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
c11draftn1570
6.3.2Otheroperands
6.3.2.1Lvalues,arrays,andfunctiondesignators
1Anlvalueisanexpression(withanobjecttypeotherthanvoid)thatpotentiallydesignatesanobject;ifanlvaluedoesnotdesignateanobjectwhenitisevaluated,thebehaviorisundefined.Whenanobjectissaidtohaveaparticulartype,thetypeisspecifiedbythelvalueusedtodesignatetheobject.Amodifiablelvalueisanlvaluethatdoesnothavearraytype,doesnothaveanincompletetype,doesnothaveaconstqualifiedtype,andifitisastructureorunion,doesnothaveanymember(including,recursively,anymemberorelementofallcontainedaggregatesorunions)withaconstqualifiedtype.
c++14draftn4296
3.10Lvaluesandrvalues[basic.lval]
1ExpressionsarecategorizedaccordingtothetaxonomyinFigure1.
(1.1)—Anlvalue(socalled,historically,becauselvaluescouldappearontheleft-handsideofanassignment
expression)designatesafunctionoranobject.[Example:IfEisanexpressionofpointertype,then
*EisanlvalueexpressionreferringtotheobjectorfunctiontowhichEpoints.Asanotherexample,
theresultofcallingafunctionwhosereturntypeisanlvaluereferenceisanlvalue.—endexample]
(1.2)—Anxvalue(an“eXpiring”value)alsoreferstoanobject,usuallyneartheendofitslifetime(sothatits
resourcesmaybemoved,forexample).Certainkindsofexpressionsinvolvingrvaluereferences(8.3.2)
yieldxvalues.[Example:Theresultofcallingafunctionwhosereturntypeisanrvaluereferenceto
anobjecttypeisanxvalue(5.2.2).—endexample]
(1.3)—Aglvalue(“generalized”lvalue)isanlvalueoranxvalue.
(1.4)—Anrvalue(socalled,historically,becauservaluescouldappearontheright-handsideofanassignment
expression)isanxvalue,atemporaryobject(12.2)orsubobjectthereof,oravaluethatisnotassociated
withanobject.
(1.5)—Aprvalue(“pure”rvalue)isanrvaluethatisnotanxvalue.[Example:Theresultofcallingafunction
whosereturntypeisnotareferenceisaprvalue.Thevalueofaliteralsuchas12,7.3e5,ortrueis
alsoaprvalue.—endexample]
Everyexpressionbelongstoexactlyoneofthefundamentalclassificationsinthistaxonomy:lvalue,xvalue,
orprvalue.Thispropertyofanexpressioniscalleditsvaluecategory.[Note:Thediscussionofeachbuilt-in
operatorinClause5indicatesthecategoryofthevalueityieldsandthevaluecategoriesoftheoperandsit
expects.Forexample,thebuilt-inassignmentoperatorsexpectthattheleftoperandisanlvalueandthat
therightoperandisaprvalueandyieldanlvalueastheresult.User-definedoperatorsarefunctions,and
thecategoriesofvaluestheyexpectandyieldaredeterminedbytheirparameterandreturntypes.—end
note]
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
c++11const是分左值跟右值的,楼上说的好这书太老了,可以扔了
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层

引用4楼fefe82的回复:Amodifiablelvalueisanlvaluethat
doesnothavearraytype,
doesnothaveanincompletetype,
doesnothaveaconstqualifiedtype,
andifitisastructureorunion,
doesnothaveanymember
(including,recursively,anymemberorelementof
allcontainedaggregatesorunions)withaconstqualifiedtype.

这句话可以这么理解么?
可修改左值,是个左值
不具有数组类型,不具有不完全类型,不具有常量性类型。
并且如果是个结构或者联合
没有任何成员(包括,递归的,所包含的集合或者联合的,任何成员或者元素)有常量性类型。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
这段话里,好像有这么四层意思:
1)数组类型不是左值。
2)不完全类型不是左值,(不能定义对象?)。
3)常量性对象或者字面值,不是左值。(只要用const(,constexpr)定义,就不是左值?字面值也不是左值)
4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?)

回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
引用6楼lm_whales的回复:Quote: 引用4楼fefe82的回复:
Amodifiablelvalueisanlvaluethat
doesnothavearraytype,
doesnothaveanincompletetype,
doesnothaveaconstqualifiedtype,
andifitisastructureorunion,
doesnothaveanymember
(including,recursively,anymemberorelementof
allcontainedaggregatesorunions)withaconstqualifiedtype.

这句话可以这么理解么?
可修改左值,是个左值
不具有数组类型,不具有不完全类型,不具有常量性类型。
并且如果是个结构或者联合
没有任何成员(包括,递归的,所包含的集合或者联合的,任何成员或者元素)有常量性类型。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
这段话里,好像有这么四层意思:
1)数组类型不是左值。
2)不完全类型不是左值,(不能定义对象?)。
3)常量性对象或者字面值,不是左值。(只要用const(,constexpr)定义,就不是左值?字面值也不是左值)
4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?)

这句话解释的是modifiablelvalue,他首先是一个lvalue,并且1)2)3)4)...
这句话是在对lvalue进行分类。
1)2)3)4)都是lvalue,但不是modifiablelvalue。除了1)2)3)4)之外的lvalue,都是modifiablelvalue。
回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
const其实是编译器判断实现的常量,是一个假常量,在实际中,使用const定义一个常量,最终还是一个变量,只是在编译器内进行了检查,发现有修改则报错。出于这种原因,const其实是可修改的(全局const变量则不能修改,因为是在常量区的)。
当然,如果const变量的值是在编译时就可确定的,如constinta=5;同时代码中没有显式获取该变量的地址,那么编译器也会将其优化成一个真常量

回复

使用道具 举报

千问 | 2021-1-27 06:45:35 | 显示全部楼层
引用7楼fefe82的回复:Quote: 引用6楼lm_whales的回复:
Quote: 引用4楼fefe82的回复:
Amodifiablelvalueisanlvaluethat
doesnothavearraytype,
doesnothaveanincompletetype,
doesnothaveaconstqualifiedtype,
andifitisastructureorunion,
doesnothaveanymember
(including,recursively,anymemberorelementof
allcontainedaggregatesorunions)withaconstqualifiedtype.

这句话可以这么理解么?
可修改左值,是个左值
不具有数组类型,不具有不完全类型,不具有常量性类型。
并且如果是个结构或者联合
没有任何成员(包括,递归的,所包含的集合或者联合的,任何成员或者元素)有常量性类型。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
这段话里,好像有这么四层意思:
1)数组类型不是左值。
2)不完全类型不是左值,(不能定义对象?)。
3)常量性对象或者字面值,不是左值。(只要用const(,constexpr)定义,就不是左值?字面值也不是左值)
4)结构体或者联合的任何成员是常量性的,不是左值。(包含常量成员的结构体或者联合也不是左值?)

这句话解释的是modifiablelvalue,他首先是一个lvalue,并且1)2)3)4)...
这句话是在对lvalue进行分类。
1)2)3)4)都是lvalue,但不是modifiablelvalue。除了1)2)3)4)之外的lvalue,都是modifiablelvalue。

原来这么理解,
6.3.2.1这段话里,除了modifiablelvalue都没有突出说明
至少没有把lvalue分成modifiablelvalue和非modifiablelvalue
忽然出现一个modifiablelvalue感觉和强调lvalue就是modifiablelvalue差不多
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行