• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:

Tell the difference between lvalue and rvalue.

Hi,
 I'd like to overload a function (a constructor in particular), so that it does different things depending on whether you pass it an lvalue or an rvalue. In particular, if its an lvalue, I want the object to be a deep copy of the object which it's being passed, but if its an rvalue, then I just want the object to take on the value of the argument.

e.g. superficially
class A{
int body;
int*pointer;

A(int z):body(z){}
A(int&z):pointer(&z){}

};

This probably wont work though because

int b=5;
A a(b); // will be ambiguous.

What's the correct thing to do in these circumstances?
0
glebspy
Asked:
glebspy
1 Solution
 
nietodCommented:
You can't ovverload based on l/r.

You can overload based on const, and that is probably what you want.

For example

A:A(int &B);
A:;A(const int &B);

is legal and often useful.
0
 
nietodCommented:
I suspect we need to know more about your exact needs to give you a better answer.  From what I can tell, most likely, you are going about this the wrong way.   What exactly is this class for and what exactly are you trying to do?
0
 
tdubroffCommented:
I don't remember the details (I think it was with the [] operator), but in More Effective C++, Scott Meyers shows a way to distinguish between lvalues and rvalues.  I'm not sure if it would apply to a constructor, but you might want to check it out.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jasonclarkeCommented:
> I don't remember the details

he is describing proxy classes, and how they can be used to delay determination of whether an object is being used as an lvalue or an rvalue.  I don't think it helps in this case.

I agree that this is probably the wrong approach - this makes it quite difficult to be sure that the appropriate constructor is being used in all cases.  A more explicit approach would probably be better.
0
 
glebspyAuthor Commented:
I can overload based on const? Wow...  isn't that weird?
Suppose I have
A(const int&);
and
A(int&);

which gets called in each of the following three cases:

void main(void){int a=4;const int b=5;

A(a);//case 1
A(b);//case 2
A(6);//case 3
}
?
0
 
nietodCommented:
case 1 is the non const version,
case 2 is the constant version
case 3 is the constant version.

In C++ unammed temporaries are constant.
0
 
nietodCommented:
Think about it, the non-constant version of A can change is parameter's value and that change must be passed back to the caller.  (It may or may not make the change, the point is the fact that the parameter is a non-constant reference so it could make the change)  Thus if it were to make that change it would be chanigng a hard-coded constant.  this is conceptually illogical.   Worse it could be a problem in broader circumstances.  the point of the function might be to change the valaue of the parameter.  If the function could then be called with an unnamed temporary (which it can't) the changes would be lost, so the point of the function would be lost--most liekly by mistake.
0
 
nietodCommented:
Did this actually solve the problem then?
0
 
glebspyAuthor Commented:
I'll have to sleep on it, but you opened up a whole new line of thought.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now