Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr as reference argument
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-03-19 19:29:34


What about RVO and NRVO?

You still can bind a temporary to a const reference. This is a simple
example on how it is possible to do optimizations. Or am I mistaken?

Example:

std::string foo();

void bar()
{
   std::string const& bound_temporary = foo();

   std::cout << bound_temporary;
}

whereas this is invalid:

void invalid_bar()
{
   std::string& bound_temporary = foo(); //will not work, but AFAIK some
compilers support it
}

So compilers can do optimizations based on constness.

Regards,
Ovanes

On Fri, Mar 20, 2009 at 12:15 AM, Emil Dotchevski
<emildotchevski_at_[hidden]>wrote:

> On Thu, Mar 19, 2009 at 3:49 PM, Ovanes Markarian
> <om_boost_at_[hidden]> wrote:
> > For boost::shared_ptr it's the same. Nothing is copied in both cases.
> Some
> > compilers might (which is under doubt) optimize it better. Since const is
> an
> > additional information for them, that the object is not going to change
> > within the called function.
>
> Const in a reference is merely part of the type safety system.
> Compilers can not do optimizations based on the constness of a
> reference, all they can do is report errors.
>
> Emil Dotchevski
> Reverge Studios, Inc.
> http://www.revergestudios.com/reblog/index.php?n=ReCode
>
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net