Re: non-const reference and const reference
- From: George <George@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Dec 2007 01:59:00 -0800
Hi Abhishek,
I think what you are pointing towards is optimization rather than
convenience. And if it is optimization that you are hinting towards
then is that not what RVO/NRVO would take care of in this case?
What do you mean RVO/NRVO? Looks like there is something you think it is not
fully optimized in the following code provided by Alex before? What are the
points do you think there are issues?
std::string get_file_path();
const std::string& str = get_file_path();
// use str
FILE* f = fopen(str.c_str(), ...);
....
So, instead of making redundant copy of a string, you just
bind a reference to return value of `foo' and use it as long
as you need it.
regards,
George
"Abhishek Padmanabh" wrote:
On Dec 15, 7:32 pm, "Alex Blekhman" <tkfx.REM...@xxxxxxxxx> wrote:.
"George" wrote:
My question is almost answered. From your and Igor's
reply, the only
confusion remaining is, why a const reference could be
binded to a rvalue?
Because it's convenient. Sometimes you want to prolong the
lifetime of a temporary object. Then binding a reference to
it comes handy:
std::string get_file_path();
const std::string& str = get_file_path();
// use str
FILE* f = fopen(str.c_str(), ...);
...
So, instead of making redundant copy of a string, you just
bind a reference to return value of `foo' and use it as long
as you need it.
I think what you are pointing towards is optimization rather than
convenience. And if it is optimization that you are hinting towards
then is that not what RVO/NRVO would take care of in this case?
- Follow-Ups:
- Re: non-const reference and const reference
- From: Abhishek Padmanabh
- Re: non-const reference and const reference
- References:
- Re: non-const reference and const reference
- From: Alex Blekhman
- Re: non-const reference and const reference
- From: George
- Re: non-const reference and const reference
- From: Alex Blekhman
- Re: non-const reference and const reference
- From: Abhishek Padmanabh
- Re: non-const reference and const reference
- Prev by Date: Re: non-const reference and const reference
- Next by Date: Re: non-const reference and const reference
- Previous by thread: Re: non-const reference and const reference
- Next by thread: Re: non-const reference and const reference
- Index(es):
Relevant Pages
|