[Insight-developers] Questions about operator= in ITK

Brad King brad.king at kitware.com
Thu Mar 28 11:15:10 EDT 2013


On 03/28/2013 10:54 AM, Bradley Lowekamp wrote:
> On Mar 28, 2013, at 10:33 AM, "Williams, Norman K" <norman-k-williams at uiowa.edu> wrote:
>> 1. operator= returning void -- this seems like it's completely wrong, even
>> if the result is never used in practice.
>
> 1 ) I agree it seem wrong to me. If the method is returning void

The convention is used in places where we want to poison operator=
because it should not be called.  Typically the copy constructor
is also poisoned.  If this convention has leaked outside this use
case then those instances should be fixed.

> My personal favorite way to implement the assignment operator
> is using the copy constructor, followed by a swap.

Yes, this is *the* way to do it for exception safety.
The simplest way to do this is to arrange something like:

 X& operator=(X x) // by value so it is copy constructed!
   {
   swap(*this, x);
   return *this;
   }

-Brad


More information about the Insight-developers mailing list