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
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
  • 8
  • 19
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,146
  • 1
  • 26
  • 40
25
votes
5 answers

Why are literals and temporary variables not lvalues?

I've read that lvalues are "things with a defined storage location". And also that literals and temporaries variables are not lvalues, but no reason is given for this statement. Is it because literals and temporary variables do not have defined…
pasha
  • 2,035
  • 20
  • 34
25
votes
5 answers

Is a dereferenced pointer a valid lvalue?

Assuming the definition: int i = 10; int *p = &i; Why is *p a valid lvalue here: *p+=10; Shouldn't *p evaluate to the value of the int stored at &i, ie. 10, and hence generate a "Not an lvalue" error?
user191776
24
votes
6 answers

lvalue required as left operand of assignment

Why am I getting lvalue required as left operand of assignment with a single string comparison? How can I fix this in C? if (strcmp("hello", "hello") = 0) Thanks!
Joseph
  • 401
  • 2
  • 6
  • 10
23
votes
2 answers

Bind rvalue reference to lvalue with `void*`

While trying to understand how rvalue references work I ended up with this piece of code: int* iptr = nullptr; int*&& irr = iptr; Compiling the above code gives the following error: error: rvalue reference to type 'int *' cannot bind to lvalue of…
Nick
  • 10,309
  • 21
  • 97
  • 201
22
votes
3 answers

PODs, non-PODs, rvalue and lvalues

Could anyone explain the details in terms of rvalues, lvalues, PODs, and non-PODs the reason why the first expression marked below is not ok while the second expression marked below is ok? In my understanding both int() and A() should be rvalues,…
anonymous
  • 231
  • 2
  • 4
21
votes
3 answers

Rvalue to lvalue conversion?

Why it is not possible to convert rvalues to lvalues? It is possible to do a conversion in the opposite direction though. Technically rvalues do have a memory address, isn't it?
user8194556
20
votes
2 answers

Why are C++0x rvalue reference not the default?

One of the cool new features of the upcoming C++ standard, C++0x, are "rvalue references." An rvalue reference is similar to an lvalue (normal) reference, except that it can be bound to a temporary value (normally, a temporary can only be bound to a…
Zifre
  • 26,504
  • 11
  • 85
  • 105
19
votes
4 answers

What are the uses of lvalue subroutines in Perl?

I don't understand what could be the uses of lvalue subroutines? What is it that I can't accomplish with normal subroutines? Could you please post some examples? Thanks
Geo
  • 93,257
  • 117
  • 344
  • 520
19
votes
3 answers

Is the result of a cast an rvalue?

Let int a = 0; Then is (int)a an rvalue in standard C++? Different compilers show different results for this code: #include using namespace std; void f(int& x) { cout << "l value" << endl; } void f(int&& x) { cout << "r value"…
Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
19
votes
4 answers

Const reference and lvalue

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?
NewB
  • 344
  • 3
  • 12
18
votes
2 answers

Why are compound literals in C modifiable

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…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
18
votes
3 answers

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

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…
Mustafa
  • 1,814
  • 3
  • 17
  • 25
18
votes
3 answers

In C++11, how can I get a temporary lvalue without a name?

I have a traditional C lib and a function (setsockopts) wants an argument by pointer. In C++11 (gcc 4.8), can I pass this argument without initializing a named variable? I have the following, non-satisfying solution: #include #include…
not-a-user
  • 4,088
  • 3
  • 21
  • 37
1
2
3
44 45