From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,15edb893ef79e231 X-Google-Attributes: gidfac41,public X-Google-Thread: 109fba,748db7f53fbf40dc,start X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,15edb893ef79e231 X-Google-Attributes: gid103376,public X-Google-Thread: f4fd2,23202754c9ce78dd X-Google-Attributes: gidf4fd2,public X-Google-Thread: 114809,15edb893ef79e231 X-Google-Attributes: gid114809,public X-Google-ArrivalTime: 2002-01-24 08:53:21 PST Path: archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.bc.tac.net!news.bc.tac.net!not-for-mail Followup-To: comp.lang.c++ Sender: blaak@TORUS Newsgroups: comp.lang.lisp,comp.lang.ada,comp.lang.eiffel,comp.lang.smalltalk,comp.lang.c++ Subject: Canconical C++ assignment (was Re: True faiths ...) References: <%njZ7.279$iR.150960@news3.calgary.shaw.ca> <3c36fbc5_10@news.newsgroups.com> <4idg3u40ermnp682n6igc5gudp7hajkea9@4ax.com> <76be8851.0201101909.9db0718@posting.google.com> <9jtu3u8cq92b05j47uat3412tok6hqu1ki@4ax.com> <3C3F8689.377A9F0F@brising.com> <3219936759616091@naggum.net> <3C483CE7.D61D1BF@removeme.gst.com> <7302e4fa4a.simonwillcocks@RiscPC.enterprise.net> <3C4D9B03.60803@mail.com> <3C4DE2F3.9020904@mail.com> <3C4DF550.24D3333A@nyc.rr.com> <3C4F272B.6020209@mail.com> <3C4EA9BB.B9AB65AE@nyc.rr.com> <3C4FC2FF.7000103@mail.com> From: Ray Blaak Message-ID: Organization: The Transcend X-Newsreader: Gnus v5.7/Emacs 20.7 Date: 24 Jan 2002 08:50:54 -0800 NNTP-Posting-Host: 208.181.209.61 X-Complaints-To: news@bctel.net X-Trace: news.bc.tac.net 1011891194 208.181.209.61 (Thu, 24 Jan 2002 08:53:14 PST) NNTP-Posting-Date: Thu, 24 Jan 2002 08:53:14 PST Xref: archiver1.google.com comp.lang.lisp:25154 comp.lang.ada:19277 comp.lang.eiffel:5517 comp.lang.smalltalk:18577 comp.lang.c++:120695 Date: 2002-01-24T08:50:54-08:00 List-Id: Hyman Rosen writes: > It was in the problem spec. Anyway, the (now) canonical way to do this > ib C++, thanks to Herb Sutter's "Exceptional C++", is to make objects > have a non-throwing swap() method, and base assignment on copying: > > struct s > { > void swap(s &other) throw(); // non-throwing swap > s(const s &other); // copy constructor > s &operator=(const s &other) > { > s(other).swap(*this); > return *this; > } > }; Interesting. Is a discussion of the tradeoffs of this approach online anywhere? I note an original presentation at http://www.gotw.ca/gotw/059.htm. My only criticism with this approach is that one now has two places to maintain a detailed field-by-field knowledge of the class: in the copy constructor and in swap(). For maintenance reasons, I prefer this kind of approach, although there are downsides with exception handling and dealing with virtual vs non-virtual assignment operators: class C { public: C(const C &other) { *this = other; } C &operator=(const C &other) { if (this != &other) { // field by field assignment/cleanup, perhaps to temp vars first // to allow intelligent exception processing. } return *this; } }; -- Cheers, The Rhythm is around me, The Rhythm has control. Ray Blaak The Rhythm is inside me, blaak@telus.net The Rhythm has my soul.