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.

587 questions
211
votes
3 answers

Return type of '?:' (ternary conditional operator)

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".
Yola
  • 16,575
  • 11
  • 57
  • 92
89
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,451
  • 3
  • 27
  • 44
74
votes
3 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,177
  • 18
  • 25
53
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,070
  • 3
  • 41
  • 80
41
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,099
  • 8
  • 17
39
votes
6 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
  • 44,537
  • 22
  • 112
  • 180
35
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
  • 10,293
  • 4
  • 42
  • 88
32
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
  • 333
  • 1
  • 3
  • 8
32
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
  • 35,107
  • 16
  • 95
  • 241
31
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
  • 28,340
  • 55
  • 153
  • 242
31
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,273
  • 2
  • 13
  • 30
30
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,477
  • 11
  • 41
  • 58
30
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
  • 122
  • 314
  • 481
29
votes
3 answers

Why is it not a compiler error to assign to the result of a substr call?

I just discovered the most baffling error and I don't understand why the compiler did not flag it for me. If I write the following: string s = "abcdefghijkl"; cout << s << endl; s.substr(2,3) = "foo"; s.substr(8,1) = '.'; s.substr(9,1) = 4; cout…
blahedo
  • 834
  • 7
  • 18
27
votes
2 answers

C++0x const RValue reference as function parameter

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…
MW_dev
  • 2,137
  • 1
  • 26
  • 37
1
2 3
39 40