Bug 46696 - [C++0x] Implicit copy constructor can't construct array of subtype with user-defined copy constructor.
Summary: [C++0x] Implicit copy constructor can't construct array of subtype with user-...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: 4.6.1
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-29 03:18 UTC by Simon Hill
Modified: 2011-05-26 13:44 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-05-25 16:03:04


Attachments
Test source - erroneously fails to compile. (209 bytes, text/x-c++src)
2010-11-29 03:18 UTC, Simon Hill
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Hill 2010-11-29 03:18:30 UTC
Created attachment 22558 [details]
Test source - erroneously fails to compile.

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.


§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: Ubuntu(64) 10.4. Core2Duo(64).
Comment 1 Jason Merrill 2011-05-25 19:51:46 UTC
Author: jason
Date: Wed May 25 19:51:44 2011
New Revision: 174226

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174226
Log:
	PR c++/46696
	* typeck.c (cp_build_modify_expr): Check DECL_DEFAULTED_FN.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/defaulted29.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog
Comment 2 Jason Merrill 2011-05-25 20:29:49 UTC
Author: jason
Date: Wed May 25 20:29:47 2011
New Revision: 174233

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174233
Log:
	PR c++/46696
	* typeck.c (cp_build_modify_expr): Check DECL_DEFAULTED_FN.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/defaulted29.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/typeck.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 3 Jason Merrill 2011-05-26 13:44:54 UTC
Fixed for 4.6.1.