Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr as reference argument
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-03-20 10:58:22


Emil,

I know that the second snippet is illegal in C++, but your statement was
that const is only a part of type system and has no impact on optimization,
I just answered you with my doubts, and made a first obvious example with
NRVO, which highly depends on const.

const might also involve some optimizations, e.g. in multi-threaded context.
Let's imagine a value being passed to a function by const reference. This
function starts 2 or more threads and passes the value to them as reference
to a const object as well. What happens in this context? As long it is not
volatile it might be cached in a register, but I think that if the value is
const, compiler has stronger assumption, that this value is good enough to
be cached for all threads...

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

> On Thu, Mar 19, 2009 at 4:29 PM, Ovanes Markarian
> <om_boost_at_[hidden]> wrote:
> > 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
> > }
>
> Right, your first snippet is valid and the second is ill-formed.
>
> > So compilers can do optimizations based on constness.
>
> What optimizations do you have in mind? The second snippet is simply
> illegal C++ (should not compile.)
>
> Emil Dotchevski
> Reverge Studios, Inc.
> http://www.revergestudios.com/reblog/index.php?n=ReCode
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



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