Newsgroups: comp.lang.c++ Path: utzoo!utgpu!cunews!csi.uottawa.ca!news From: hitz@sim5.csi.uottawa.ca (Martin Hitz) Subject: Re: Semantics of default assignment operator Message-ID: <1991Apr2.201510.311@csi.uottawa.ca> Summary: It is defined, but ain't clear. Keywords: assignment operator Sender: news@csi.uottawa.ca Nntp-Posting-Host: sim5 Organization: University of Ottawa References: <1991Mar22.172508.1881@csi.uottawa.ca> <71616@microsoft.UUCP> Distribution: na Date: Tue, 2 Apr 91 20:15:10 GMT In article <71616@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >In article <1991Mar22.172508.1881@csi.uottawa.ca> hitz@sim5.csi.uottawa.ca (Martin Hitz) writes: >|I recently came across the following incompatibility between >|C and C++: >|If a structure X contains an array, then the assignment >| >| struct X a, b; >| /* ..... */ >| b = a; >| >|is well defined in C and older C++ versions (bitwise copy). >| >|Now, as C++ adopted the memberwise assignment semantics, it is not clear >|to me what should happen, as there is no such thing as assignment to >|arrays. > >I agree that ARM is not very clear on this issue. However, see section >12.8 > >Generated copy constructors and op= now use memberwise assignment. So >the generated op= should come down to a part that does a: > >b.array = a.array; > >Which is undefined. Therefore, I claim that at compile-time the compiler >should squawk that the array assignment is undefined in the generated >copy constructor. > >One way around this dilemma would be for C++ to support the pure extension over >C to allow array assignment. Such could be enabled in C++ since in C++ >arrays and references to arrays include a concept of the size of the >array. > [paragraph on benefits deleted] I asked Bjarne Stroustroup for his opinion on the assignment of arrays nested within structures: MH>What does "memberwise assignment" (in the absence of an explicit MH>assignment operator) mean for class members that are (ordinary) arrays? BS> that every element of the member array is copied. So, I believe it *is* defined (BTW, g++ did it correctly, Zortech did it wrong). However, to my knowledge the ARM lacks an explicit mention of the fact that memberwise assignment is *recursively* applied (which is implicitely clear for structures, but not for arrays). Martin (hitz@csi.UOttawa.CA)