From: Leigh Johnston on 16 Feb 2010 05:35 Hi, I am slightly worried by the example in 3.8.7, specifically the following line: new (this) C(other); // new object of type C created Although not the case in this example if C's copy constructor could throw an exception a destructor could be erroneously called (destruction of c1 in the example during stack unwinding). I think the following change would "fix" this or make it less confusing at least: new (this) C(other); // new object of type C created becomes new (this) C(other); // new object of type C created (C's implicitly defined copy constructor should never throw an exception) /Leigh -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++(a)netlab.cs.rpi.edu] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
From: Pete Becker on 16 Feb 2010 07:04 Leigh Johnston wrote: > Hi, > > I am slightly worried by the example in 3.8.7, specifically the following > line: > > new (this) C(other); // new object of type C created > > Although not the case in this example if C's copy constructor could throw > an > exception a destructor could be erroneously called (destruction of c1 in > the > example during stack unwinding). > > I think the following change would "fix" this or make it less confusing at > least: > > new (this) C(other); // new object of type C created > > becomes > > new (this) C(other); // new object of type C created (C's implicitly > defined > copy constructor should never throw an exception) > > Unfortunately, people seem to read this example of how lifetimes work as recommending this approach to coding. Anybody who writes code like this deserves whatever happens to them. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference" (www.petebecker.com/tr1book) [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++(a)netlab.cs.rpi.edu<std-c%2B%2B(a)netlab.cs.rpi.edu> ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
From: Goran on 17 Feb 2010 00:17 On Feb 16, 11:35 pm, "Leigh Johnston" <le...(a)i42.co.uk> wrote: > Hi, > > I am slightly worried by the example in 3.8.7, specifically the following > line: > > new (this) C(other); // new object of type C created > > Although not the case in this example if C's copy constructor could throw an > exception a destructor could be erroneously called (destruction of c1 in the > example during stack unwinding). What you say is correct. What's done there is __not__ what one should do as a normal course of action. One __can__ do that when C(const C&) does not throw. But even then, that line you've shown most often likely should be preceded by this->~C(). That is, "this" should likely be "destroyed" as a normal course of action (although one could imagine a situation where even this is not needed). Correct way is to have assignment operator and do simply *this = other; Wrong reasons why people do this sort of thing: * they don't know any better * laziness (prick couldn't be bothered to write assignment operator) * premature optimization ("I know, I'll recreate object in-place, that'll be faster!") Goran. P.S. What's 3.8.7. and N3000, BTW? -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++(a)netlab.cs.rpi.edu] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
From: Leigh Johnston on 17 Feb 2010 05:24 > Correct way is to have assignment operator and do simply > > *this = other; > > I disagree, this results in infinite recursion (or finite with stack fault given a finite stack size)! You probably meant something slightly different. :) /Leigh -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++(a)netlab.cs.rpi.edu<std-c%2B%2B(a)netlab.cs.rpi.edu> ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
From: Pete Becker on 17 Feb 2010 05:27 Goran wrote: > On Feb 16, 11:35 pm, "Leigh Johnston" <le...(a)i42.co.uk> wrote: > >> Hi, >> >> I am slightly worried by the example in 3.8.7, specifically the following >> line: >> >> new (this) C(other); // new object of type C created >> >> Although not the case in this example if C's copy constructor could throw >> an >> exception a destructor could be erroneously called (destruction of c1 in >> the >> example during stack unwinding). >> > > What you say is correct. What's done there is __not__ what one should > do as a normal course of action. One __can__ do that when C(const C&) > does not throw. But even then, that line you've shown most often > likely should be preceded by this->~C(). That is, "this" should likely > be "destroyed" as a normal course of action (although one could > imagine a situation where even this is not needed). > > Correct way is to have assignment operator and do simply > > *this = other; > > Wrong reasons why people do this sort of thing: > * they don't know any better > * laziness (prick couldn't be bothered to write assignment operator) > * premature optimization ("I know, I'll recreate object in-place, > that'll be faster!") > > Or "wow, that's cool!" Goran. > > P.S. What's 3.8.7. and N3000, BTW? > > > N3000 is the current working draft for C++0x (there's a newer one in the pipeline, but it's a couple days away at the moment). 3.8.7 is the section number of the text under discussion. Well, actually, it should be 3.8/7, that is, paragraph 7 in section 3.8. If you have the current standard, it's 3.8/5. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference" (www.petebecker.com/tr1book) [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++(a)netlab.cs.rpi.edu<std-c%2B%2B(a)netlab.cs.rpi.edu> ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
|
Next
|
Last
Pages: 1 2 Prev: Why does inserter need Iterator template parameter Next: GPP Generic Preprocessor |