0

I read in accelerated c++ that we should pass lvalue to non-const references. Why is that? As far as i know lvalue is the one which can be modified. Please tell me if it has another definition.

I checked it by writing a simple program:

#include<iostream>
#include<vector>
using namespace std;
vector<double> ret()
{
    vector<double> d;
    return d;
}
void rec(vector<double> &g)
{
    cout<<"entered..."<<endl;
}
int main
{
    rec(ret());
}

And error is

invalid initialization of non const reference from r value

What meaning do rvalue and lvalue have here? d is a local variable in ret() and it is passed by value so it does not have dangling issues.

What happens when rec(ret()) is called and explain to me how rec() sends its argument.

virusai
  • 27
  • 1
  • 7
  • possible duplicate of [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](http://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – Slava Mar 18 '15 at 17:55
  • http://en.cppreference.com/w/cpp/language/value_category – Dmitry Ledentsov Mar 18 '15 at 18:00

1 Answers1

1

You said,

as far as i know lvalue is the one which cannot be modified.

That is not correct. Traditionally, lvalue stood for a value which can appear on the left hand side of an assignment operator, which means they must be modifiable.

That meaning is still valid today.

Perhaps you are confusing with rvalue, which stood for a value which can appear on the right hand side of assignment operator, which may or may not be modifiable.

That meaning is still valid today.

EDIT

Now that you changed your question to say:

as far as i know lvalue is the one which can be modified.

The answer should be obvious to you. when the argument type of a function is a non-const reference, only lvalues can be passed to such functions, not rvalues.

EDIT 2

Thanks to the comment by @0x499602D2:

For user-defined classes you can use the assignment operator on rvalues as they are like member function calls: A{} = A{}; would compile given an accessible copy/move-assignment operator. I think the term lvalue designates an object that will exist beyond the expression in which it is used, and rvalue meaning the opposite.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • that' my writing mistake...my question is how can some same say a 'd 'is rvalue... – virusai Mar 18 '15 at 18:08
  • @virusai, Here's a link to another SO post addresses that question in some depth: http://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues. – R Sahu Mar 18 '15 at 18:11
  • Rvalues can appear on the left hand side of an assignment. – David G Mar 18 '15 at 18:15
  • @0x499602D2, please elaborate. – R Sahu Mar 18 '15 at 18:23
  • For user-defined classes you can use the assignment operator on rvalues as they are like member function calls: `A() = A()` would compile given an accessible copy/move-assignment operator. I think the term lvalue means an object that will exist beyond the expression in which it is used, and rvalue meaning the opposite. – David G Mar 18 '15 at 18:25