Questions tagged [lvalue]

L-value represents the address of the value. "L" stands for the left side, because the address it is what is required when the variable appears on the left side of an assignment operation.

661 questions
215
votes
4 answers

What is the result type of '?:' (ternary/conditional operator)?

Why does the first conditional operator result in a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; However, the second does not. int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second does not compile at all: error: lvalue…
Yola
  • 18,496
  • 11
  • 65
  • 106
92
votes
9 answers

Why doesn't a+++++b work?

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 =…
Barshan Das
  • 3,677
  • 4
  • 32
  • 46
83
votes
4 answers

C++: is return value a L-value?

Consider this code: struct foo { int a; }; foo q() { foo f; f.a =4; return f;} int main() { foo i; i.a = 5; q() = i; } No compiler complains about it, even Clang. Why q() = ... line is correct?
John
  • 2,295
  • 1
  • 20
  • 25
58
votes
1 answer

std::is_same different results between compilers

#include int main() { bool b = true; std::cout << std::is_same::value << "\n"; auto bb = (!(!b)); std::cout << std::is_same::value << "\n"; } The above code has different…
Nir
  • 1,608
  • 1
  • 17
  • 29
54
votes
4 answers

How to determine programmatically if an expression is rvalue or lvalue in C++?

What's the best way to determine if an expression is a rvalue or lvalue in C++? Probably, this is not useful in practice but since I am learning rvalues and lvalues I thought it would be nice to have a function is_lvalue which returns true if the…
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
51
votes
7 answers

Function that accepts both lvalue and rvalue arguments

Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template? For example, suppose I write a function print_stream that reads from an istream and prints the data that was read to the screen,…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
43
votes
1 answer

Expression must be a modifiable L-value

I have here char text[60]; Then I do in an if: if(number == 2) text = "awesome"; else text = "you fail"; and it always said expression must be a modifiable L-value.
Mysterigs
  • 443
  • 1
  • 4
  • 8
43
votes
3 answers

Why and when does the ternary operator return an lvalue?

For a long time I thought that the ternary operator always returns an rvalue. But to my surprise it doesn't. In the following code I don't see the difference between the return value of foo and the return value of the ternary operator. #include…
Soulimane Mammar
  • 1,658
  • 12
  • 20
42
votes
4 answers

Rvalue Reference is Treated as an Lvalue?

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…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
39
votes
3 answers

what is return type of assignment operator?

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…
oczkoisse
  • 1,561
  • 3
  • 17
  • 31
36
votes
1 answer

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

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 struct A{ void gun() const&{std::cout << "gun const&" << std::endl;} void gun()…
alfC
  • 14,261
  • 4
  • 67
  • 118
32
votes
1 answer

How to change the value of an NSMutableArray at a particular index

[array objectAtIndex:i] doesn't work as an L value, so it can't be used to set the object at index i.
node ninja
  • 31,796
  • 59
  • 166
  • 254
31
votes
3 answers

Why pre-increment operator gives rvalue in C?

In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why?
Happy Mittal
  • 3,667
  • 12
  • 44
  • 60
31
votes
6 answers

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

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…
hpsMouse
  • 2,004
  • 15
  • 20
31
votes
3 answers

Are literal strings and function return values lvalues or rvalues?

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?
Tim
  • 1
  • 141
  • 372
  • 590
1
2 3
44 45