I think I may have hit a bug where an implicit copy constructor can't construct an array of a subtype with a user-defined copy constructor. I can't see any hits searching for "invalid array assignment" on the bug repository. I messed up submitting my last bug so I thought I'd ask here first for confirmation.
§12.8.28 states: "A copy/move assignment operator that is defaulted and not defined as deleted is implicitly defined when [...] or when it is explicitly defaulted after its first declaration." §12.8.30 (implicitly-defined copy assignment) states: "The implicitly-defined copy assignment operator for a non-union class X performs memberwise copy assignment of its subobjects [...] Each subobject is assigned in the manner appropriate to its type: [...] -- if the subobject is an array, each element is assigned, in the manner appropriate to the element type;" I'm assuming that "the manner appropriate to the element type" means use copy-assignment. At least, that's what seems to happens if the main object's copy-assignment operator is implicitly defined. Yet the above doesn't seem able to compile if: - The main object contains an array of the subobject. - The main object's copy-assignment operator IS explicitly defaulted (§12.8.28). - The subobject's copy-assignment operator isn't implicitly or default defined. TEST SOURCE (Attached): 1) I created the most trivial type (named SFoo) that contains a non-default copy-assignment operator. 2) I created the most trivial type (named SBar) that contains: - an array of SFoo. - an explicitly defaulted copy-assignment operator. 3) I created a function that: - creates two copies of SBar. - assigns one copy to the other. TEST: I compiled using the -std=c++0x option. GCC refuses to compile (11:8: error: invalid array assignment). - If I remove the explicit defaulting of SBar's copy-assignment, it works. - If I default SFoo's copy-assignment, it works. SPECS: GCC: 4.6.0 20101106 (experimental) (GCC) - Using Pedro Lamarão's delegating constructors patch: - http://gcc.gnu.org/ml/gcc-patches/2007-04/msg00620.html - (I can't see this having any effect here). TARGET: x86_64-unknown-linux-gnu SYSTEM: Core2Duo(64), Ubuntu(64) 10.4. TL/DR: (§12.8.28) & (§12.8.30) seem to say attached code should compile. It doesn't.
struct SFoo { SFoo& operator = (SFoo const&) { return *this; } // <--(1) FAILS. // =default; // <--(2) WORKS. //void operator = (SFoo const&) {} // <--(3) ALSO FAILS. }; struct SBar { SBar& operator = (SBar const&) =default; // <--(4): WORKS if removed. SFoo M_data[1]; }; int main() { SBar x; SBar y; y = x; }