用双引号括起来的字符串是属于lvalue还是rvalue

qq_40162781 2019-12-31 03:50:02
RT、

用双引号括起来的字符串是属于左值还是右值
使用printf("%p",&"dfsdf");输出了地址
...全文
199 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zxy0918520 2020-01-02
  • 打赏
  • 举报
回复
引用 4 楼 不闻窗外事 的回复:
左值啊,"dfsdf"就是常量嘛,常量都是左值,就好比const int a = 5;你也可以打印a的地址啊,但是它还是左值
在C里面叫做constants,在C++里叫做literal,constants在C++里用到的大概只有巴科斯范式和constant expression, 还有只有string-literal 是一个lvalue,其他的都是rvalue
Zxy0918520 2020-01-02
  • 打赏
  • 举报
回复
首先,谢邀! ------------------------------------------------------------------------------------------------------------------------------- 楼主首先要搞清楚双引号括起来的字符串是什么,在C++里是literal(s), 在c里面它与之对应的应该叫做"Constant(s)" C++17 Standard paper 5.13 Literals additional context 17) The term “literal” generally designates, in this document, those tokens that are called “constants” in ISO C. 5.13.5 String literals [lex.string] string-literal: encoding-prefixopt " s-char-sequenceopt " encoding-prefixopt R raw-string s-char-sequence: s-char s-char-sequence s-char s-char: any member of the source character set except the double-quote ", backslash \, or new-line character escape-sequence universal-character-name raw-string: " d-char-sequenceopt ( r-char-sequenceopt ) d-char-sequenceopt " r-char-sequence: r-char r-char-sequence r-char § 5.13.5 19 ©ISO/IEC N4713 r-char: any member of the source character set, except a right parenthesis ) followed by the initial d-char-sequence (which may be empty) followed by a double quote ". d-char-sequence: d-char d-char-sequence d-char d-char: any member of the basic source character set except: space, the left parenthesis (, the right parenthesis ), the backslash \, and the control characters representing horizontal tab, vertical tab, form feed, and newline. 1 A string-literal is a sequence of characters (as defined in 5.13.3) surrounded by double quotes, optionally prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in "...", R"(...)", u8"...", u8R"**(...)**", u"...", uR"*~(...)*~", U"...", UR"zzz(...)zzz", L"...", or LR"(...)", respectively. 8 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration 然后: 表达式大概可以分为: 8.2 Properties of expressions [expr.prop] 8.2.1 Value category [basic.lval] 1 Expressions are categorized according to the taxonomy in Figure 1. expression glvalue rvalue lvalue xvalue prvalue Figure 1 — Expression category taxonomy 整理一下就是: expression->glvalue(泛左值),rvalue(右值) gvalue->lvalue(左值),xvalue(“eXpiring” lvalues:将亡值). rvalue->prvalue(pure rvalue:纯右值),xvalue(同上) 最后重点: 8.4.1 Literals [expr.prim.literal] 1 A literal is a primary expression. Its type depends on its form (5.13). A string literal is an lvalue; all other literals are prvalues
lin5161678 2019-12-31
  • 打赏
  • 举报
回复
C里面分辨是不是左值太简单了
刚刚好你就做了
能加一元&运算符的表达式 全部是左值
不能加一元&运算符的表达式 全部不是左值
"123"是左值
能加&
&"123" 不是左值
不能加&
& &"123" 这种做法是错的
包括 const int n = 10;
这个n 也是左值
不闻窗外事 2019-12-31
  • 打赏
  • 举报
回复
引用 5 楼 不闻窗外事 的回复:
[quote=引用 4 楼 不闻窗外事 的回复:]
左值啊,"dfsdf"就是常量嘛,常量都是左值,就好比const int a = 5;你也可以打印a的地址啊,但是它还是左值

草,我说错了,是右值[/quote]
把我第一个回答的左值都换成右值,头有点晕,想错了
不闻窗外事 2019-12-31
  • 打赏
  • 举报
回复
引用 4 楼 不闻窗外事 的回复:
左值啊,"dfsdf"就是常量嘛,常量都是左值,就好比const int a = 5;你也可以打印a的地址啊,但是它还是左值

草,我说错了,是右值
不闻窗外事 2019-12-31
  • 打赏
  • 举报
回复
左值啊,"dfsdf"就是常量嘛,常量都是左值,就好比const int a = 5;你也可以打印a的地址啊,但是它还是左值
寻开心 2019-12-31
  • 打赏
  • 举报
回复
&“dfsdf” 加了&之后,就不是左值了。 没加之前,它是。
寻开心 2019-12-31
  • 打赏
  • 举报
回复
左值是用来指明一个对象的表达式。
最简单的左值就是变量名称。左值(lvalue)之所以称为“左”(以首字母为 L,代表 left),是因为一个左值表示一个对象,它可以出现在赋值运算符(assignment operator)的左边,例如“左表达式=右表达式”。

其他表达式(那些表示一个值但不指明一个对象的),被类似地称为右值(rvalue)。
右值是可以出现在赋值运算符右边而不是左边的表达式。例如,常量和算术表达式。

“dfsdf”是不是一个对象? 它是!
尽管它位于只读区域, 它不可更改, 但是它确实是一个对象。
能解析出对应对象的地址的你都可以认为是左值。
lin5161678 2019-12-31
  • 打赏
  • 举报
回复
左值

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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