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
4
votes
5 answers

What's the deal with temporary objects and references?

One can frequently read that you cannot bind normal lvalue reference to temporary object. Because of that one can frequently see methods of class A taking const A& as a parameter when they don't want to involve copying. However such construct is…
Argbart
  • 41
  • 1
  • 2
4
votes
2 answers

How to pass lvalue to function taking rvalue only without templates

My question is simple #include using namespace std; template void f(T&& i) { cout << i << endl; } void g(int&& i) { cout << i << endl; } int main() { int i = 0; f(i); // works fine …
4
votes
2 answers

Why my object still gets copied when I tried to return a reference

class Obj { public: Obj(int aa, int bb): a(aa), b(bb) {} Obj(const Obj& o) {a = o.a; b = o.b;std::cout << "copying" << std::endl;} Obj(Obj&& o) {a = o.a; b = o.b;std::cout << "moving" << std::endl;} int a; int b; }; const Obj&…
coin cheung
  • 949
  • 2
  • 10
  • 25
4
votes
2 answers

"Expression resolves to an unused l-value" vs "Expression is unused"

Consider the following code: class Foo { let bar = "Hello world!" init () { self // Warning: Expression of type 'Foo' is unused self.bar // Error: Expression resolves to an unused l-value } func test () { …
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
4
votes
5 answers

Why rvalue reference as return type can't be initialization of non-const reference?

I read this question and I know that an rvalue referenec is an lvalue. However, for this code, example 1, int &&fun() { return 1; } int main() { int &a = fun(); } When I compile it: error: invalid initialization of non-const reference of…
namasikanam
  • 256
  • 3
  • 10
4
votes
3 answers

C++ lvalues and rvalues in template functions

I took sample from http://www.cplusplus.com/reference/utility/forward: // forward example #include // std::forward #include // std::cout // function with lvalue and rvalue reference overloads: void overloaded (const…
J. S.
  • 425
  • 4
  • 13
4
votes
2 answers

CPP Reference in Constructor and Function

Am bit confused on the below code. ` class sample { public: sample() { } sample( sample& Obj ) { } }; void fun( sample& Obj ) { } int main() { sample s(sample()); fun( sample() ); return 0; } am getting the below…
4
votes
2 answers

Universal reference template type always evaluate to lvalue

My code: #include #include #include void store(std::string & val) { std::cout << "lvalue " << val << '\n'; } void store(std::string && val) { std::cout << "rvalue " << val << '\n'; } template void…
demalegabi
  • 577
  • 6
  • 19
4
votes
5 answers

Why ++(*p) is not giving l-value required error?

#include int main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; ++*p; p += 2; printf("%d", *p); return 0; } Why is this code not giving any compile time error,My doubt is ++*p is evaluated as ++(*p) and *p will be…
Geeta
  • 91
  • 1
4
votes
1 answer

c++ is const pointer to literal valid?

I know lvalues can be converted into const reference. I'm curious if I can get a pointer to those lvalues. If I write const int* p = &3; //ERROR: lvalue required as unary operand '&' I get this error. However, const int* p = &((const int&)3); this…
eivour
  • 1,678
  • 12
  • 20
4
votes
2 answers

C++ - lvalue required as left operand of assignment

Consider the following code: #include using namespace std; class X { int i; public: X(int ii = 0); }; X::X(int ii) { i = ii; } int a; X f1() { return X(); } int f2() { return a; } int main() { f1() = X(1); f2() =…
George Cernat
  • 1,323
  • 9
  • 27
4
votes
5 answers

Is always the address of a reference equal to the address of origin?

This looks a very basic topic but very important to me. The following example shows that the address of a reference variable is equal to the address of the original variable. I know this is what we can expect from the concept of C/C++. However, is…
ar2015
  • 5,558
  • 8
  • 53
  • 110
4
votes
2 answers

Why does std::forward converts lvalue and rvalue to rvalue reference?

I suppose I am confused with std::forward. My function which uses std::forward is following, but it is much simplified and modified to make explanation easily. // This is an example code to explain my question simply. template
mora
  • 2,217
  • 4
  • 22
  • 32
4
votes
0 answers

How to programmatically check if an expression is r-value or l-value in C (possibly non-standard)

Suppose I would like to write a preprocessor macro which would have a conditional branch inside built on whether an argument to that macro is l-value or r-value. Is that achievable with C (not C++, not C11), possibly with GCC extensions?
Wojciech Migda
  • 738
  • 7
  • 16
4
votes
1 answer

"error: lvalue required as left operand of assignment" in conditional operator

I'm new to C and today I learnt "?" operator which is the short type of if-else statement. However, when I execute this code: int b; int x; b=3<2?x=12:x=34; I get an error "error: lvalue required as left operand of assignment". I don't understand…
F. Eser
  • 125
  • 9