From 4388612562280083857 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,f5d433881cc29cff X-Google-Attributes: gidf78e5,public From: Mattias Lundstroem Subject: Re: Named return value optimisation- reprise. Date: 1997/06/17 Message-ID: <33A65B20.595D@aom.ericsson.se>#1/1 X-Deja-AN: 249035352 References: <33a345bf.7425526@news.ipswich.gil.com.au> <33A478C1.329@acm.org> X-Original-Date: Tue, 17 Jun 1997 11:38:40 +0200 Organization: Ericsson Hewlett-Packard Telecom AB X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBM6ZsguEDnX0m9pzZAQHJHQF+JSTznOUU6sd+QTij78x2rW+UBfmKLYht QzrQuLnWbIj+bkMe9N/8SVLIY1hIZrjU =SJIZ Newsgroups: comp.std.c++ Originator: fjh@mundook.cs.mu.OZ.AU Jason Merrill wrote: > I thought we discussed this before and decided that 12.8 was referring to > the actual object itself, not the object and everything it points to. So > the example would produce surprising results in the presence of the RVO. > > But I'm OK with that. I'm comfortable saying that playing games with > references and values in the same class, and relying on the copy > constructor to switch between them, is asking for trouble. > > Jason Have I missed something important in the discussions of the NRVO? How about you have one class that refers to the internals of another class. This is a rather common pattern with perhaps the most clear example being containers and iterators. My understanding is that the rule could break code like that supplied by Brian Parker(below). Did anyone refute this? --8<------------------------------------------------------------------------------------ // example of faulty paragraph 15 #include #include #include #include using namespace std; vector reverse_vec(vector v) { // v passed by-value, so we can safely modify it in-place reverse(v.begin(), v.end()); return v; } int main() { vector v(4); v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4; vector::iterator it1 = v.begin(); vector::iterator it2 = v.end(); // vector v is never (directly) used again, and it1 and it2 point to internal // dynamically-allocated memory i.e. not to any part of the object v, // so by 12.8 [class.copy] paragraph 15 the call to reverse_vec can be silently // changed by the compiler to pass by-reference vector v2 = reverse_vec(v); transform(it1, it2, v2.begin(), ostream_iterator(cout, ","), plus()); // would expect output to be "5,5,5,5," // but [class.copy] paragraph 15 allows "8,6,4,2," to be output! return 0; } --- [ comp.std.c++ is moderated. To submit articles: try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ] [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ] [ Comments? mailto:std-c++-request@ncar.ucar.edu ]