xref: /llvm-project/clang/test/CXX/special/class.copy/p11.0x.copy.cpp (revision afe48f9d68e446829bed6ce72b319c915873809a)
19ca5c425SRichard Smith // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
21bc6f71eSAlexis Hunt 
3c91d12ceSRichard Smith struct Trivial {};
41bc6f71eSAlexis Hunt struct NonTrivial {
51bc6f71eSAlexis Hunt   NonTrivial(const NonTrivial&);
61bc6f71eSAlexis Hunt };
71bc6f71eSAlexis Hunt 
8522fa537SRichard Smith // A defaulted copy constructor for a class X is defined as deleted if X has:
9522fa537SRichard Smith 
10522fa537SRichard Smith // -- a variant member with a non-trivial corresponding constructor
11852265ffSRichard Smith union DeletedNTVariant {
12c5f98f34SRichard Smith   NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}
131bc6f71eSAlexis Hunt   DeletedNTVariant();
141bc6f71eSAlexis Hunt };
151bc6f71eSAlexis Hunt DeletedNTVariant DVa;
1674f7d50fSDouglas Gregor DeletedNTVariant DVb(DVa); // expected-error{{call to implicitly-deleted copy constructor}}
171bc6f71eSAlexis Hunt 
18852265ffSRichard Smith struct DeletedNTVariant2 {
191bc6f71eSAlexis Hunt   union {
20c5f98f34SRichard Smith     NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant2' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}
211bc6f71eSAlexis Hunt   };
221bc6f71eSAlexis Hunt   DeletedNTVariant2();
231bc6f71eSAlexis Hunt };
241bc6f71eSAlexis Hunt DeletedNTVariant2 DV2a;
2574f7d50fSDouglas Gregor DeletedNTVariant2 DV2b(DV2a); // expected-error{{call to implicitly-deleted copy constructor}}
261bc6f71eSAlexis Hunt 
27522fa537SRichard Smith // -- a non-static data member of class type M (or array thereof) that cannot be
28522fa537SRichard Smith //    copied because overload resolution results in an ambiguity or a function
29522fa537SRichard Smith //    that is deleted or inaccessible
301bc6f71eSAlexis Hunt struct NoAccess {
311bc6f71eSAlexis Hunt   NoAccess() = default;
321bc6f71eSAlexis Hunt private:
331bc6f71eSAlexis Hunt   NoAccess(const NoAccess&);
341bc6f71eSAlexis Hunt 
351bc6f71eSAlexis Hunt   friend struct HasAccess;
361bc6f71eSAlexis Hunt };
371bc6f71eSAlexis Hunt 
38852265ffSRichard Smith struct HasNoAccess {
39852265ffSRichard Smith   NoAccess NA; // expected-note{{copy constructor of 'HasNoAccess' is implicitly deleted because field 'NA' has an inaccessible copy constructor}}
401bc6f71eSAlexis Hunt };
411bc6f71eSAlexis Hunt HasNoAccess HNAa;
4274f7d50fSDouglas Gregor HasNoAccess HNAb(HNAa); // expected-error{{call to implicitly-deleted copy constructor}}
431bc6f71eSAlexis Hunt 
441bc6f71eSAlexis Hunt struct HasAccess {
451bc6f71eSAlexis Hunt   NoAccess NA;
461bc6f71eSAlexis Hunt };
471bc6f71eSAlexis Hunt 
481bc6f71eSAlexis Hunt HasAccess HAa;
491bc6f71eSAlexis Hunt HasAccess HAb(HAa);
501bc6f71eSAlexis Hunt 
511bc6f71eSAlexis Hunt struct NonConst {
521bc6f71eSAlexis Hunt   NonConst(NonConst&);
531bc6f71eSAlexis Hunt };
541bc6f71eSAlexis Hunt struct Ambiguity {
551bc6f71eSAlexis Hunt   Ambiguity(const Ambiguity&);
561bc6f71eSAlexis Hunt   Ambiguity(volatile Ambiguity&);
571bc6f71eSAlexis Hunt };
581bc6f71eSAlexis Hunt 
59852265ffSRichard Smith struct IsAmbiguous {
601bc6f71eSAlexis Hunt   NonConst NC;
61852265ffSRichard Smith   Ambiguity A; // expected-note 2{{copy constructor of 'IsAmbiguous' is implicitly deleted because field 'A' has multiple copy constructors}}
621bc6f71eSAlexis Hunt   IsAmbiguous();
631bc6f71eSAlexis Hunt };
641bc6f71eSAlexis Hunt IsAmbiguous IAa;
6574f7d50fSDouglas Gregor IsAmbiguous IAb(IAa); // expected-error{{call to implicitly-deleted copy constructor}}
661bc6f71eSAlexis Hunt 
67852265ffSRichard Smith struct Deleted {
68852265ffSRichard Smith   IsAmbiguous IA; // expected-note{{copy constructor of 'Deleted' is implicitly deleted because field 'IA' has a deleted copy constructor}}
691bc6f71eSAlexis Hunt };
701bc6f71eSAlexis Hunt Deleted Da;
7174f7d50fSDouglas Gregor Deleted Db(Da); // expected-error{{call to implicitly-deleted copy constructor}}
721bc6f71eSAlexis Hunt 
73c91d12ceSRichard Smith // It's implied (but not stated) that this also applies in the case where
74c91d12ceSRichard Smith // overload resolution would fail.
75c91d12ceSRichard Smith struct VolatileMember {
76c91d12ceSRichard Smith   volatile Trivial vm; // expected-note {{has no copy}}
77c91d12ceSRichard Smith } vm1, vm2(vm1); // expected-error {{deleted}}
78c91d12ceSRichard Smith 
79522fa537SRichard Smith // -- a direct or virtual base class B that cannot be copied because overload
80522fa537SRichard Smith //    resolution results in an ambiguity or a function that is deleted or
81522fa537SRichard Smith //    inaccessible
82852265ffSRichard Smith struct AmbiguousCopyBase : Ambiguity { // expected-note 2{{copy constructor of 'AmbiguousCopyBase' is implicitly deleted because base class 'Ambiguity' has multiple copy constructors}}
83522fa537SRichard Smith   NonConst NC;
84522fa537SRichard Smith };
85522fa537SRichard Smith extern AmbiguousCopyBase ACBa;
86522fa537SRichard Smith AmbiguousCopyBase ACBb(ACBa); // expected-error {{deleted copy constructor}}
87522fa537SRichard Smith 
88852265ffSRichard Smith struct DeletedCopyBase : AmbiguousCopyBase {}; // expected-note {{copy constructor of 'DeletedCopyBase' is implicitly deleted because base class 'AmbiguousCopyBase' has a deleted copy constructor}}
89522fa537SRichard Smith extern DeletedCopyBase DCBa;
90522fa537SRichard Smith DeletedCopyBase DCBb(DCBa); // expected-error {{deleted copy constructor}}
91522fa537SRichard Smith 
92852265ffSRichard Smith struct InaccessibleCopyBase : NoAccess {}; // expected-note {{copy constructor of 'InaccessibleCopyBase' is implicitly deleted because base class 'NoAccess' has an inaccessible copy constructor}}
93522fa537SRichard Smith extern InaccessibleCopyBase ICBa;
94522fa537SRichard Smith InaccessibleCopyBase ICBb(ICBa); // expected-error {{deleted copy constructor}}
95522fa537SRichard Smith 
96522fa537SRichard Smith // -- any direct or virtual base class or non-static data member of a type with
97522fa537SRichard Smith //    a destructor that is deleted or inaccessible
981bc6f71eSAlexis Hunt struct NoAccessDtor {
991bc6f71eSAlexis Hunt private:
1001bc6f71eSAlexis Hunt   ~NoAccessDtor();
1011bc6f71eSAlexis Hunt   friend struct HasAccessDtor;
1021bc6f71eSAlexis Hunt };
1031bc6f71eSAlexis Hunt 
104852265ffSRichard Smith struct HasNoAccessDtor {
105852265ffSRichard Smith   NoAccessDtor NAD; // expected-note{{copy constructor of 'HasNoAccessDtor' is implicitly deleted because field 'NAD' has an inaccessible destructor}}
1061bc6f71eSAlexis Hunt   HasNoAccessDtor();
1071bc6f71eSAlexis Hunt   ~HasNoAccessDtor();
1081bc6f71eSAlexis Hunt };
1091bc6f71eSAlexis Hunt HasNoAccessDtor HNADa;
11074f7d50fSDouglas Gregor HasNoAccessDtor HNADb(HNADa); // expected-error{{call to implicitly-deleted copy constructor}}
1111bc6f71eSAlexis Hunt 
1121bc6f71eSAlexis Hunt struct HasAccessDtor {
1131bc6f71eSAlexis Hunt   NoAccessDtor NAD;
1141bc6f71eSAlexis Hunt };
1151bc6f71eSAlexis Hunt HasAccessDtor HADa;
1161bc6f71eSAlexis Hunt HasAccessDtor HADb(HADa);
1171bc6f71eSAlexis Hunt 
118852265ffSRichard Smith struct HasNoAccessDtorBase : NoAccessDtor { // expected-note{{copy constructor of 'HasNoAccessDtorBase' is implicitly deleted because base class 'NoAccessDtor' has an inaccessible destructor}}
119522fa537SRichard Smith };
120522fa537SRichard Smith extern HasNoAccessDtorBase HNADBa;
121522fa537SRichard Smith HasNoAccessDtorBase HNADBb(HNADBa); // expected-error{{implicitly-deleted copy constructor}}
122522fa537SRichard Smith 
123522fa537SRichard Smith // -- a non-static data member of rvalue reference type
124d87aab93SRichard Smith int some_int;
125852265ffSRichard Smith struct RValue {
126d87aab93SRichard Smith   int && ri = static_cast<int&&>(some_int); // expected-note{{copy constructor of 'RValue' is implicitly deleted because field 'ri' is of rvalue reference type 'int &&'}}
1271bc6f71eSAlexis Hunt };
1281bc6f71eSAlexis Hunt RValue RVa;
12974f7d50fSDouglas Gregor RValue RVb(RVa); // expected-error{{call to implicitly-deleted copy constructor}}
130af136f81SRichard Smith 
131*afe48f9dSRichard Smith // FIXME: The error on the class-name is attached to the location of the
132d87aab93SRichard Smith // constructor. This is not especially clear.
133*afe48f9dSRichard Smith struct RValueTmp { // expected-error {{reference member 'ri' binds to a temporary}}
134*afe48f9dSRichard Smith   int && ri = 1; // expected-note{{copy constructor of 'RValueTmp' is implicitly deleted because field 'ri' is of rvalue reference type 'int &&'}} // expected-note {{default member init}}
135d87aab93SRichard Smith };
136d87aab93SRichard Smith RValueTmp RVTa; // expected-note {{implicit default constructor for 'RValueTmp' first required here}}
137d87aab93SRichard Smith RValueTmp RVTb(RVTa); // expected-error{{call to implicitly-deleted copy constructor}}
138d87aab93SRichard Smith 
139af136f81SRichard Smith namespace PR13381 {
140af136f81SRichard Smith   struct S {
141af136f81SRichard Smith     S(const S&);
142af136f81SRichard Smith     S(const volatile S&) = delete; // expected-note{{deleted here}}
143af136f81SRichard Smith   };
144af136f81SRichard Smith   struct T {
145af136f81SRichard Smith     volatile S s; // expected-note{{field 's' has a deleted copy constructor}}
146af136f81SRichard Smith   };
147af136f81SRichard Smith   T &f();
148af136f81SRichard Smith   T t = f(); // expected-error{{call to implicitly-deleted copy constructor}}
149af136f81SRichard Smith }
15041c35d6dSRichard Smith 
15141c35d6dSRichard Smith namespace Mutable {
15241c35d6dSRichard Smith   struct A {
15341c35d6dSRichard Smith     A(const A &);
154566184acSRichard Smith     A(A &) = delete; // expected-note {{deleted here}}
15541c35d6dSRichard Smith   };
15641c35d6dSRichard Smith 
15741c35d6dSRichard Smith   struct B {
15841c35d6dSRichard Smith     A a;
15941c35d6dSRichard Smith     B(const B &);
16041c35d6dSRichard Smith   };
16141c35d6dSRichard Smith   B::B(const B &) = default;
16241c35d6dSRichard Smith 
16341c35d6dSRichard Smith   struct C {
164566184acSRichard Smith     mutable A a; // expected-note {{deleted because field 'a' has a deleted copy constructor}}
16541c35d6dSRichard Smith     C(const C &);
16641c35d6dSRichard Smith   };
16741c35d6dSRichard Smith   C::C(const C &) = default; // expected-error{{would delete}}
16841c35d6dSRichard Smith }
169