lvalue

Can a function's return value be an lvalue in JavaScript?

Deadly 提交于 2019-12-23 13:09:03
问题 Microsoft allows to set environment variables in JScript with the following syntax: var sh = WScript.CreateObject("Wscript.Shell"); var env = sh.Environment("PROCESS"); env("TEST") = "testvalue"; I wonder about the third line - and with me JSLint, which calls this line a "Bad Assigment". But it works! Is it ECMAscript standard compatible to have a function's return value as an lvalue (like here)? If yes: How would one write such a function? 回答1: Yes, the standard permits functions to returns

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

亡梦爱人 提交于 2019-12-22 11:12:06
问题 I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference,

Reference initialization in C++

筅森魡賤 提交于 2019-12-22 04:26:34
问题 Can anybody explain to me why there is a difference between these two statements? class A{}; const A& a = A(); // correct A& b = A(); // wrong It says invalid initialization of non-const reference of type A& from a temporary of type A Why does const matter here? 回答1: Non-const references must be initialised with l-values. If you could initialise them with temporaries, then what would the following do? int& foo = 5; foo = 6; // ?! const references have the special property that they extend the

lvalue required as left operand of assignment error when using C

旧城冷巷雨未停 提交于 2019-12-20 11:37:28
问题 int main() { int x[3]={4,5,6}; int *p=x; p +1=p;/*compiler shows error saying lvalue required as left operand of assignment*/ cout<<p 1; getch(); } 回答1: When you have an assignment operator in a statement, the LHS of the operator must be something the language calls an lvalue . If the LHS of the operator does not evaluate to an lvalue , the value from the RHS cannot be assigned to the LHS. You cannot use: 10 = 20; since 10 does not evaluate to an lvalue . You can use: int i; i = 20; since i

Is std::move(*this) a good pattern?

别说谁变了你拦得住时间么 提交于 2019-12-20 11:07:09
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

Is std::move(*this) a good pattern?

白昼怎懂夜的黑 提交于 2019-12-20 11:05:28
问题 In order to make this code with C++11 reference qualifiers work as expected I have to introduce a std::move(*this) that doesn't sound right. #include<iostream> struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun() &&{std::cout << "gun&&" << std::endl;} void fun() const&{gun();} void fun() &&{std::move(*this).gun();} // <-- is this correct? or is there a better option }; int main(){ A a; a.fun(); // prints gun const& A().fun(); // prints gun&& } Something doesn't

What does `int const a[5]` really mean?

梦想与她 提交于 2019-12-20 02:05:28
问题 Consider the following array declaration: int const a[5]; From the semantic standpoint of the language, is it exactly equivalent to const int a[5] ? Assuming that is the case, both declarations would essentially read like " a is an array of 5 constant ints ". The alternative way to read the first declaration would be " a is a constant array of 5 ints ". Obviously, both of the statements logically imply that the whole array is constant; if an array consists of 5 constant ints, then the entire

What does `int const a[5]` really mean?

Deadly 提交于 2019-12-20 02:05:12
问题 Consider the following array declaration: int const a[5]; From the semantic standpoint of the language, is it exactly equivalent to const int a[5] ? Assuming that is the case, both declarations would essentially read like " a is an array of 5 constant ints ". The alternative way to read the first declaration would be " a is a constant array of 5 ints ". Obviously, both of the statements logically imply that the whole array is constant; if an array consists of 5 constant ints, then the entire

C++0x rvalue references - lvalues-rvalue binding

被刻印的时光 ゝ 提交于 2019-12-18 12:54:39
问题 This is a follow-on question to C++0x rvalue references and temporaries In the previous question, I asked how this code should work: void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } It seems that the move overload should probably be called because of the implicit temporary, and this happens in GCC but not MSVC (or the EDG front-end used in MSVC's Intellisense). What about this code? void f(std::string &&); //NB: No

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

℡╲_俬逩灬. 提交于 2019-12-18 08:16:40
问题 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