- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在网上看了很多,好像很多人都提到了下面的规则(但我在标准中找不到),
加法运算符 +(以及所有其他二元运算符)要求两个操作数都是右值,结果也是右值。等等..
我查看了 C++ 标准,它明确指出(条款 3.10/2),
Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue
(第 5/9 条),
Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue.
它使用一个术语,操作数“期望”一个纯右值。然而,当我查看加法运算符、乘法运算符等时,它只提到结果是纯右值,但没有说明操作数是“预期”的。
二元运算符是否真的期望操作数是纯右值在以下情况下会有所不同,
int b = 2;
int a = b + 1;
如果b期望是prvalue,这里会进行左值到右值的转换,然后执行prvalue + prvalue返回一个prvalue,结果prvalue赋给一个左值a。
但是,如果 b 不需要是纯右值,则它将是左值 + 纯右值,结果是纯右值。
我真的很想知道标准在哪里明确或隐含地提到了不同运营商的规则?我检查了所有运算符部分,只有少数运算符标准明确提到操作数和结果是左值还是右值。对于大多数运算符,标准仅提及结果而未提及操作数要求。
谢谢。
顺便说一句,我在标准 5.19 中发现关于常量表达式的内容可能非常非常“隐含地”暗示二元运算符需要对操作数进行左值到右值的转换。更多详情,请引用我之前的问题,
mixing use of constexpr and const?
A conditional-expression is a constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2).
...
— an lvalue-to-rvalue conversion (4.1) unless it is applied to
———— a glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression
感谢阅读。
最佳答案
因此,这通常是标准的那些推断和不明确的部分之一;然而,在 3.10
[ Note: some built-in operators expect lvalue operands. [ Example: built-in assignment operators all expect their left-hand operands to be lvalues. — end example ] Other built-in operators yield rvalues, and some expect them. [ Example: the unary and binary + operators expect rvalue arguments and yield rvalue results. — end example ] The discussion of each built-in operator in clause 5 indicates whether it expects lvalue operands and whether it yields an lvalue. — end note ]
请注意“在第 5 条中指示它是否需要左值操作数以及它是否产生左值”的不明确的语言。
第 5 章的检查表明,每个表达式需要或返回左值的情况都被枚举,但是很少有专门处理右值的情况被枚举,我认为然后假定其余的被假定为右值。
我还怀疑它的指定不当,因为从标准的角度来看,转换是由运算符隐式或显式完成的并不是特别重要,无论如何,行为应该是一致且行为良好的。
关于c++ - 关于左值到右值的转换,什么时候需要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5099769/
我是 C++ 的新手,正在尝试编写如下接口(interface): template class Comparable { protected: Comparable(){};
什么是左值? 最佳答案 左值是可以分配给以下值的值: lvalue = rvalue; 它是“left value”或“lefthand value”的缩写,它基本上只是=符号左侧的值,即您为其分配值
这个问题已经有答案了: Why do C and C++ support memberwise assignment of arrays within structs, but not general
C++03 $14.1/6 - "A non-type non-reference template-parameter is not an lvalue." C++0x $14.2/6- "A no
这只是出于好奇我问... 我在任何地方看到的 99% 的代码,当使用“IF”时,将采用“If (RValue == LValue) ...”格式。例子: If (variableABC == "Hel
在 C++03 中,表达式可以是右值,也可以是左值。 在 C++11 中,表达式可以是: 右值 左值 x值 gl值 右值 两个类别变成了五个类别。 这些新的表达类别是什么? 这些新类别与现有的右值和左
我正在尝试将 div 向左移动指定数量的像素,我正在使用从此处获得的 slider : http://carpe.ambiprospect.com/slider/archive/v1.3/ 所以从示例
所以我有一个函数需要将 std::vector 作为参数。我想知道声明参数的最佳方式,这样底层数组就不会被深度复制,因为它可能相当大。 // which should I choose? void m
在 C++03 中,表达式要么是右值,要么是左值。 在 C++11 中,表达式可以是: 右值 左值 xvalue glvalue 右值 两类变成了五类。 这些新的表达类别是什么? 这些新类别与现有的右
我正在尝试实现类似 find 的方法来从容器中提取对值的引用,如果找不到该值或该值的类型不兼容,则返回一个默认值。 template const T& get (key_type k, const T
我有以下代码: #include using namespace std; void test(int& a) { cout << "lvalue." << endl; } void tes
我的搜索发现许多关于右值绑定(bind)到左值的帖子,但没有任何类似的帖子。对不起,如果它是重复的。 struct StrHolder { StrHolder(std::string&& s)
这是一个后续问题 C++0x rvalue references and temporaries 在上一个问题中,我问过这段代码应该如何工作: void f(const std::string &);
为什么这不起作用: ostream& operator' lvalue to 'std::basic_ostream&&'| error: initializing argument 1 of '
偶尔,我被问到一个技巧问题:x++ 和 ++x 哪个不能在 c 中留值?很多人告诉我++x不能,因为++x的汇编代码不返回寄存器。我对此表示怀疑。所以我做了一些实验。 C 代码: #include
所以这个问题与我为实际问题选择的解决方案有关,为了尽可能提供完整的图片,这个问题是这样的:基本上我有一个带有很多模板参数的函数,我正在尝试暴露给 Python。与用宏做一些噩梦般的事情来消除我需要的所
指针很简单。有一些内存保存着一个地址。要获得(有意义的)值,取消引用返回地址指向的内存中包含的值。 引用以某种方式做一些类似的事情:它们持有指向一个临时对象的“链接”。但是当我分配或使用引用时会发生什
下面是一些示例代码: #include class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() {
背景 以下代码块出现在 Scott Meyers 的名著 "Effective C++" 中第 3 项: class TextBlock { public: ... const cha
我是一名优秀的程序员,十分优秀!