lvalue

One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

放肆的年华 提交于 2019-11-26 15:31:24
string foo() { return "hello"; } int main() { //below should be illegal for binding a non-const (lvalue) reference to a rvalue string& tem = foo(); //below should be the correct one as only const reference can be bind to rvalue(most important const) const string& constTem = foo(); } GCC is the good one to give a compile error : invalid initialization of non-const reference of type std::string& from a temporary of type std::string VS2008 is not too bad as at least it gives a compile warning : warning C4239: nonstandard extension used : 'initializing' : conversion from std::string to std::string

should std::common_type use std::decay?

老子叫甜甜 提交于 2019-11-26 14:24:16
问题 Given types A,B , I am concerned with the exact definition of std::common_type<A,B> , disregarding the variadic case std::common_type<A...> for arbitrary types A... . So let using T = decltype(true ? std::declval<A>() : std::declval<B>()); using C = std::common_type<A,B>; Now, according to a number of sources, I have found the following relations (skipping typename for brevity): cppreference.com: C::type = std::decay<T>::type cplusplus.com: C::type = T GCC 4.8.1 <type_traits> implementation:

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

青春壹個敷衍的年華 提交于 2019-11-26 12:43:35
问题 I was reading Thomas Becker\'s article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue. This sounds very reasonable to me. It also clearly identifies the rvalueness of an rvalue reference. My questions are: Do you agree with this rule? If not, can you give an example where this

Is it valid to bind non-const lvalue-references to rvalues in C++ 11?(modified)

此生再无相见时 提交于 2019-11-26 09:57:24
问题 I know in c++03, an an non-const reference cannot be bound to rvalues. T& t = getT(); is invalid, and in c++11, we can do this: T&& t = getT(); but what about the above code, should that work in c++11? I tested the codes below with vs11: Foo getFoo() { return Foo(); } void fz(Foo& f) { } int getInt() { return int(); } void iz(int& i) { } int main() { { Foo& z = getFoo(); //ok fz(getFoo()); //ok int& z2 = getInt(); //error: initial value of reference to non-const must be an lvalue iz(getInt())

Why array type object is not modifiable?

随声附和 提交于 2019-11-26 07:35:54
问题 It is stated here that The term modifiable lvalue is used to emphasize that the lvalue allows the designated object to be changed as well as examined. The following object types are lvalues, but not modifiable lvalues: An array type An incomplete type A const-qualified type A structure or union type with one of its members qualified as a const type Because these lvalues are not modifiable, they cannot appear on the left side of an assignment statement. Why array type object is not modifiable?

Are literal strings and function return values lvalues or rvalues?

帅比萌擦擦* 提交于 2019-11-26 04:44:25
问题 Just wonder if a literal string is an lvalue or an rvalue. Are other literals (like int, float, char etc) lvalue or rvalue? Is the return value of a function an lvalue or rvalue? How do you tell the difference? 回答1: string literals are lvalues, but you can't change them rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue The C standard recognizes the original terms stood for left and right as in L = R ; however, it says to think of lvalue as locator value , which

Why doesn&#39;t a+++++b work?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 01:22:50
问题 int main () { int a = 5,b = 2; printf(\"%d\",a+++++b); return 0; } This code gives the following error: error: lvalue required as increment operand But if I put spaces throughout a++ + and ++b , then it works fine. int main () { int a = 5,b = 2; printf(\"%d\",a++ + ++b); return 0; } What does the error mean in the first example? 回答1: printf("%d",a+++++b); is interpreted as (a++)++ + b according to the Maximal Munch Rule ! . ++ (postfix) doesn't evaluate to an lvalue but it requires its

Return type of &#39;?:&#39; (ternary conditional operator)

爷,独闯天下 提交于 2019-11-26 00:58:33
问题 Why does the first return a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; While the second does not? int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second did not compile at all - \"not lvalue left of assignment\". 回答1: Expressions don't have return types, they have a type and - as it's known in the latest C++ standard - a value category. A conditional expression can be an lvalue or an rvalue . This is its value category. (This is somewhat of a simplification, in C++11

Rvalue Reference is Treated as an Lvalue?

有些话、适合烂在心里 提交于 2019-11-26 00:29:21
问题 I posted this answer: https://stackoverflow.com/a/28459180/2642059 Which contains the following code: void foo(string&& bar){ string* temp = &bar; cout << *temp << \" @:\" << temp << endl; } Is bar an rvalue or an lvalue? I ask because I obviously cannot take the address of an rvalue, yet I can take the address of an rvalue reference as is done here. If you can perform any operation on an rvalue reference that you can on an lvalue reference what is the point in differentiating between the two

Return type of &#39;?:&#39; (ternary conditional operator)

心不动则不痛 提交于 2019-11-25 20:57:13
Why does the first return a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; While the second does not? int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second did not compile at all - "not lvalue left of assignment". CB Bailey Expressions don't have return types, they have a type and - as it's known in the latest C++ standard - a value category. A conditional expression can be an lvalue or an rvalue . This is its value category. (This is somewhat of a simplification, in C++11 we have lvalues, xvalues and prvalues.) In very broad and simple terms, an lvalue refers to an