lvalue

Why isn't this rvalue promoted to an lvalue as specified in the reference?

冷暖自知 提交于 2019-12-18 08:16:07
问题 The Rust Reference says: The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...] When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead. This rvalue promotion obviously works with borrowing: let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts

how to assign to the names() attribute of the value of a variable in R

浪子不回头ぞ 提交于 2019-12-18 04:27:22
问题 In R, "assign('x',v)" sets the object whose name is 'x' to v. Replace 'x' by the result of applying a text function to a variable x. Then "assign" shows its worth. Unfortunately, "assign(paste('names(','x',')',sep=''),v)" fails. So if 'x' is a variable x, I can set its value, but I can't give it names for its elements. Can one work around this? a parse-eval trick maybe? Thanks. 回答1: In the form you ask question there is no need to assign names. If you x exists then you do names(x) <- v . This

What is dynamic type of object

馋奶兔 提交于 2019-12-18 02:51:37
问题 What i think is that dynamic type means dynamically allocated object using new . In the following case, do you say p points to dynamic type or static type of object? In standard, it doesn't say about dynamic type being dynamic object. 1.3.3 - The type of the most derived object (1.8) to which the lvalue denoted by an lvalue expression refers. [Example: if a pointer (8.3.1) p whose static type is "pointer to class B" is pointing to an object of class D, derived from B (clause 10), the dynamic

Why pre-increment operator gives rvalue in C?

百般思念 提交于 2019-12-17 22:57:49
问题 In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why? 回答1: C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.1/2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation . The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ <=> rvalue However for

Const reference and lvalue [duplicate]

雨燕双飞 提交于 2019-12-17 22:43:43
问题 This question already has answers here : Literal initialization for const references (3 answers) Closed 5 years ago . We cannot write int& ref = 40 because we need lvalue on right side. But we can write const int& ref = 40 . Why is this possible? 40 is rvalue instead lvalue I know that this is an exception but why? 回答1: As Stroustrup says: The initializer for a const T& need not be an lvalue or even of type T. In such cases: [1] First, implicit type conversion to T is applied if necessary. [2

Regarding lvalue-to-rvalue conversion, when is it required?

大兔子大兔子 提交于 2019-12-17 20:22:21
问题 I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on.. I checked the C++ standard, and it clearly states that (clause 3.10/2), Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue (clause 5/9), Whenever a glvalue

C++0x const RValue reference as function parameter

∥☆過路亽.° 提交于 2019-12-17 16:43:13
问题 I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std

Is a member of an rvalue structure an rvalue or lvalue?

ⅰ亾dé卋堺 提交于 2019-12-17 16:13:29
问题 A function call returning a structure is an rvalue expression, but what about its members? This piece of code works well with my g++ compiler, but gcc gives a error saying "lvalue required as left operand of assignment": struct A { int v; }; struct A fun() { struct A tmp; return tmp; } int main() { fun().v = 1; } gcc treats fun().v as rvalue, and I can understand that. But g++ doesn't think the assignment expression is wrong. Does that mean fun1().v is lvalue in C++? Now the problem is, I

Why are compound literals in C modifiable

不想你离开。 提交于 2019-12-17 10:06:50
问题 One does usually associate 'unmodifiable' with the term literal char* str = "Hello World!"; *str = 'B'; // Bus Error! However when using compound literals, I quickly discovered they are completely modifiable (and looking at the generated machine code, you see they are pushed on the stack): char* str = (char[]){"Hello World"}; *str = 'B'; // A-Okay! I'm compiling with clang-703.0.29 . Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if

what is return type of assignment operator?

て烟熏妆下的殇ゞ 提交于 2019-12-17 06:38:16
问题 I am just starting C++. I am a bit confused about the return type of assignment and dereference operator. I am following the book C++ Primer. At various occasions, the author says that the return type of assignment operator is reference to the type of left hand operand but later on, he says that the return type is the type of the left hand operand. I have referred C++11 Standard Sec. 5.17, where the return type is described as "lvalue referring to left hand operand". Similarly, I can't figure