shared_ptr and pass by value
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; } ~~~~~~~~~~~~~~~~~~~ Regards, Marat.
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.
participants (2)
-
Marat Abrarov
-
Peter Dimov