non-const reference and const reference | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | |
Hello everyone,
This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks.
1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.
2. A non-const reference can not binded to a rvalue, I think the reason is rvalue is not addressable? And we can not change the rvalue through its reference? Are there any other reasons? I am not quite sure whether my understanding is fully correct. Since there are some non-modifiable lvalues (so we do not always need to modify values through its reference). I am still studying what is the reason in essence in compiler why a non-const reference can not be binded to a rvalue.
3. Both const and non-const reference can be binded to a lvalue.
thanks in advance,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: non-const reference and const reference
I'm not sure what you are talking about : -
int main()
-
{
-
int data = 25;
-
const int& rcdata = data;
-
int& rdata = data;
-
-
data = rdata; //non-const reference as rvalue
-
data = rcdata; //const reference as rvalue
-
}
-
Here a variable is assgined to itself using references as rvalues.
Also a reference does not extend the life of a variable. If you return a reference of any kind to a local variable in a function, the reference is garbage when the local variable of the function is deleted. The sad thing is that the compiler may no delete the local variable immeidiately so your code appears to work- maybe.
Try to not make up rules around references. A reference is just an alternate name for a variable. If the reference is const, then the variable is considerend const when you use the reference (whether is variable is really const is irrelevant). If the reference is not const then the variable is considered not const. If the variable is really const and the referecne is not const, then you will get a compiler error trying to make a non-const reference to a const variable. If the compiler allows it, you could use the non-const referecne to chnage the valus of the const variable. That's a big no-no.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
Thanks weaknessforcats,
Your reply is great! But I can not see your direct answer to my original 3 questons. :-) Quote:
Originally Posted by weaknessforcats I'm not sure what you are talking about: -
int main()
-
{
-
int data = 25;
-
const int& rcdata = data;
-
int& rdata = data;
-
-
data = rdata; //non-const reference as rvalue
-
data = rcdata; //const reference as rvalue
-
}
-
Here a variable is assgined to itself using references as rvalues.
Also a reference does not extend the life of a variable. If you return a reference of any kind to a local variable in a function, the reference is garbage when the local variable of the function is deleted. The sad thing is that the compiler may no delete the local variable immeidiately so your code appears to work- maybe.
Try to not make up rules around references. A reference is just an alternate name for a variable. If the reference is const, then the variable is considerend const when you use the reference (whether is variable is really const is irrelevant). If the reference is not const then the variable is considered not const. If the variable is really const and the referecne is not const, then you will get a compiler error trying to make a non-const reference to a const variable. If the compiler allows it, you could use the non-const referecne to chnage the valus of the const variable. That's a big no-no.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: non-const reference and const reference
The code sample isn't enough?? What else would you like me to say?
|  | Expert | | Join Date: Feb 2007
Posts: 430
| | | re: non-const reference and const reference
#1 sounds like something that might be supported in Java, but not in C++. Read what W4cats said about this topic.
As for the other two questions, I'm with W4cats, I'm not sure what your are asking. A couple of examples (and descriptions) for each question would help.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
Thanks weaknessforcats,
My question is answered. Quote:
Originally Posted by weaknessforcats The code sample isn't enough?? What else would you like me to say?
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
Thanks RRick, Quote:
Originally Posted by RRick #1 sounds like something that might be supported in Java, but not in C++. Read what W4cats said about this topic.
As for the other two questions, I'm with W4cats, I'm not sure what your are asking. A couple of examples (and descriptions) for each question would help.
My question is answered.
regards,
George
| | Newbie | | Join Date: Jan 2008
Posts: 1
| | | re: non-const reference and const reference Quote:
Originally Posted by RRick #1 sounds like something that might be supported in Java, but not in C++. Read what W4cats said about this topic.
As for the other two questions, I'm with W4cats, I'm not sure what your are asking. A couple of examples (and descriptions) for each question would help. RRick: #1 (reference to const extends the lifetime of a temp to the lifetime of the reference) is explicitly provided for by the C++ standard.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: non-const reference and const reference Quote:
Originally Posted by qatal RRick: #1 (reference to const extends the lifetime of a temp to the lifetime of the reference) is explicitly provided for by the C++ standard. It does not.
A reference is an alias for the original variable. The variable will continue to exist as long as its scope permits. The reference does not change that. The lifetime of the temp is an oxymoron since there is no temp variable. The referecne name goes out of scope at the end of the function.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
I do not agree with you, weaknessforcats! I think he means const reference. :-)
Please refer to Bjarne's book, section 5.5 there is sample which verifies the life of temporary object is prolonged until the life of the const reference. Quote:
Originally Posted by weaknessforcats It does not.
A reference is an alias for the original variable. The variable will continue to exist as long as its scope permits. The reference does not change that. The lifetime of the temp is an oxymoron since there is no temp variable. The referecne name goes out of scope at the end of the function.
regards,
George
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: non-const reference and const reference
Read that carefully. Bjarne is a lawyer.
A const reference often requires the creation of a compiler-created temporary object. It is guaranteed that this temprary will exist as long as the reference exists.
This does not mean that if you return a reference to a local variable that the life of the local variable is extended. You will get the same indeterminate behavior as if you returned the address of a local variable and tried to use it.
|  | Expert | | Join Date: Feb 2007
Posts: 430
| | | re: non-const reference and const reference
We need to be careful of the terms we use. I think the key term here is "temporary variable". Returning a reference to a local variable does not fail under that category.
The original post talked about extending the life a variable, but it did not limit all the cases to just a temporary variable. This post sounds like we are mixing the two types (temporary and local) together and they can't be compared this way in terms of a reference.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
You are correct, weaknessforcats!
1.
Sorry I am talking about other things in previous post. I agree with you know. And I think if we return const reference to the local variable, it should be ok, right?
2.
This will result in creation of a temporary object and return const reference to the temporary obejct, right? Quote:
Originally Posted by weaknessforcats Read that carefully. Bjarne is a lawyer.
A const reference often requires the creation of a compiler-created temporary object. It is guaranteed that this temprary will exist as long as the reference exists.
This does not mean that if you return a reference to a local variable that the life of the local variable is extended. You will get the same indeterminate behavior as if you returned the address of a local variable and tried to use it.
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
Thanks RRick,
I agree with you. I think the previous confusion is not temporary object, but function local object in weaknessforcats's post #9. Quote:
Originally Posted by RRick We need to be careful of the terms we use. I think the key term here is "temporary variable". Returning a reference to a local variable does not fail under that category.
The original post talked about extending the life a variable, but it did not limit all the cases to just a temporary variable. This post sounds like we are mixing the two types (temporary and local) together and they can't be compared this way in terms of a reference.
regards,
George
| | Newbie | | Join Date: Jan 2008
Posts: 2
| | | re: non-const reference and const reference Quote:
Originally Posted by weaknessforcats Also a reference does not extend the life of a variable. If you return a reference of any kind to a local variable in a function, the reference is garbage when the local variable of the function is deleted. The sad thing is that the compiler may no delete the local variable immeidiately so your code appears to work- maybe. Do you really teach C++? I'm scared for your students. A const reference extends the life of a temporary from the statement which created the temporary to the scope of the const reference. This is the case e.g., when a reference is held to a temporary which has been returned from a function. You are correct that it does not extend the life of the object if you try to return it outside the function which declared the const reference- but this is clear from Stroustroup, because the const reference which held the temporary goes out of scope when the function exits.
e.g., -
struct Point
-
{
-
int x;
-
int y;
-
Point( int x_, int y_ ) : x(x_),y(y_){}
-
}
-
-
Point
-
makeMyPoint()
-
{
-
return Point( 3, 4 );
-
}
-
-
void
-
someFunction()
-
{
-
// statement 1
-
const Point& p = makeMyPoint();
-
-
// statement 2
-
int distanceFromOrigin =
-
sqrt( (p.x*p.x) + (p.y*p.y) );
-
}
-
In this case the temporary "Point" object returned by the function "makeMyPoint()" would usually cease to exist at the end of statement 1. But, when a const reference is held that temporary continues to exist as long as the scope of _that_ const reference exists. In this case, the temporary remains until "someFunction()" returns because the const Point& "p" remains in scope until "someFunction()" returns. Quote:
Try to not make up rules around references.
Normally I don't mind much people being wrong alone, nor do I too much mind this kind of arrogance- as long as you're right. But being both wrong and arrogant together is a bad combination.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,366
| | | re: non-const reference and const reference Quote:
Originally Posted by micah4 Do you really teach C++? I'm scared for your students. A const reference extends the life of a temporary from the statement which created the temporary to the scope of the const reference. I do. And your example is what I am talking about : Quote:
Originally Posted by micah4 Point makeMyPoint()
{
return Point( 3, 4 );
}
void
someFunction()
{
// statement 1
const Point& p = makeMyPoint(); The temprary returned by makeMyPoint will exist as long as the referecne does. Your example is off the point.
This will not work: -
Point& makeMyPoint()
-
{
-
Point obj(3,4);
-
return obj;}
-
-
void
-
someFunction()
-
{
-
// statement 1
-
const Point& p = makeMyPoint();
-
That is, returning a reference to a local variable does not extend the scope of the local variable.
Go back and read my Post #11 again.
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
Thanks micah4,
All of your reply is great! And I agree. :-)
Below you mentioned, what means "if you try to return it outside the function which declared the const reference"? I can not understand the scenario you mean, could you show more description or show some pseudo code please? Quote:
Originally Posted by micah4 You are correct that it does not extend the life of the object if you try to return it outside the function which declared the const reference
regards,
George
| | Familiar Sight | | Join Date: Dec 2007
Posts: 200
| | | re: non-const reference and const reference
I agree, weaknessforcats.
We are talking about two things at this topic. :-) Quote:
Originally Posted by weaknessforcats I do. And your example is what I am talking about:
The temprary returned by makeMyPoint will exist as long as the referecne does. Your example is off the point.
This will not work: -
Point& makeMyPoint()
-
{
-
Point obj(3,4);
-
return obj;}
-
-
void
-
someFunction()
-
{
-
// statement 1
-
const Point& p = makeMyPoint();
-
That is, returning a reference to a local variable does not extend the scope of the local variable.
Go back and read my Post #11 again.
regards,
George
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|