[Insight-developers] Questions about operator= in ITK
Williams, Norman K
norman-k-williams at uiowa.edu
Thu Mar 28 11:29:42 EDT 2013
There is a standard way to poison assignment and copy constructor, which
is to declare them protected and then not implement them. This is
implemented consistently across all classes that derive ultimately from
itk::LightObject.
The only thing void operator= poisons is chain assignment, and the reason
it hasn't been a problem so far is that no one has used chain assignment
with those particular classes.
The copy/swap paradigm is recommended a lot of places. I haven't
encountered a case in ITK that it would make a difference. The usual
pattern suggested is
X& operator=(const X &x)
{
X tmp(x);
swap(*this,x);
return *this;
}
On 3/28/13 10:15 AM, "Brad King" <brad.king at kitware.com> wrote:
>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
>_______________________________________________
>Powered by www.kitware.com
>
>Visit other Kitware open-source projects at
>http://www.kitware.com/opensource/opensource.html
>
>Kitware offers ITK Training Courses, for more information visit:
>http://kitware.com/products/protraining.php
>
>Please keep messages on-topic and check the ITK FAQ at:
>http://www.itk.org/Wiki/ITK_FAQ
>
>Follow this link to subscribe/unsubscribe:
>http://www.itk.org/mailman/listinfo/insight-developers
________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________
More information about the Insight-developers
mailing list