Re: non-const reference and const reference

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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?

.



Relevant Pages

  • Re: non-const reference and const reference
    ... What do you mean RVO/NRVO? ... RVO expands to return value optimization. ... bound to a reference to const 'str'. ... a copy construction of another_string from the returned string object ...
    (microsoft.public.vc.language)
  • Re: non-const reference and const reference
    ... What do you mean RVO/NRVO? ... RVO expands to return value optimization. ... bound to a reference to const 'str'. ... a copy construction of another_string from the returned string object ...
    (microsoft.public.vc.language)
  • Re: Boxing and Unboxing ??
    ... This aspect is not a matter of optimization, it is a matter of preventing ... which is to pass "in" value types by reference ... I can see the merits of applying a "const" attribute to reference types ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Efficiently passing strings?
    ... > I'm instantiating the same string object twice (once for the parameter ... first is probably fine after optimization. ... compiler; there's really no other way to tell. ... using a reference exposes some of your implementation. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Here it is...proof Dave is wrong....
    ... "Optimization of Chemical Processes" by Edgar & Himmelblau is, ... can readily recommend as a reference. ... "Basic Engineering Circuit Analysis" by Irwin is, for the most part, a good ... The introductory course used the one by Popov with the advanced course using an older edition of Seely & Smith. ...
    (sci.research.careers)