Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr and pass by value
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-09-18 11:44:21


Marat Abrarov wrote:
> Dear Boost C++ Libraries Developers,
>
> According to
> http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ why
> Boost use this (not the only example):
>
> ~~~~~~~~~~~~~~~~~~~
> shared_ptr & operator=( shared_ptr const & r )
> {
> this_type(r).swap(*this);
> return *this;
> }
> ~~~~~~~~~~~~~~~~~~~
>
> and not such:
> ~~~~~~~~~~~~~~~~~~~
> shared_ptr & operator=( shared_ptr r )
> {
> r.swap(*this);
> return *this;
> }

In theory, the second version is better. However, since it's so rarely used,
it's prone to compiler bugs. The first version is more conservative. This
will probably change when by-value assignments (and by-value arguments in
general) become more popular.


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