169 lines · cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s2 3struct Trivial {};4struct NonTrivial {5 NonTrivial(const NonTrivial&);6};7 8// A defaulted copy constructor for a class X is defined as deleted if X has:9 10// -- a variant member with a non-trivial corresponding constructor11union DeletedNTVariant {12 NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}13 DeletedNTVariant();14};15DeletedNTVariant DVa;16DeletedNTVariant DVb(DVa); // expected-error{{call to implicitly-deleted copy constructor}}17 18struct DeletedNTVariant2 {19 union {20 NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant2' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}21 };22 DeletedNTVariant2();23};24DeletedNTVariant2 DV2a;25DeletedNTVariant2 DV2b(DV2a); // expected-error{{call to implicitly-deleted copy constructor}}26 27// -- a non-static data member of class type M (or array thereof) that cannot be28// copied because overload resolution results in an ambiguity or a function29// that is deleted or inaccessible30struct NoAccess {31 NoAccess() = default;32private:33 NoAccess(const NoAccess&);34 35 friend struct HasAccess;36};37 38struct HasNoAccess {39 NoAccess NA; // expected-note{{copy constructor of 'HasNoAccess' is implicitly deleted because field 'NA' has an inaccessible copy constructor}}40};41HasNoAccess HNAa;42HasNoAccess HNAb(HNAa); // expected-error{{call to implicitly-deleted copy constructor}}43 44struct HasAccess {45 NoAccess NA;46};47 48HasAccess HAa;49HasAccess HAb(HAa);50 51struct NonConst {52 NonConst(NonConst&);53};54struct Ambiguity {55 Ambiguity(const Ambiguity&);56 Ambiguity(volatile Ambiguity&);57};58 59struct IsAmbiguous {60 NonConst NC;61 Ambiguity A; // expected-note 2{{copy constructor of 'IsAmbiguous' is implicitly deleted because field 'A' has multiple copy constructors}}62 IsAmbiguous();63};64IsAmbiguous IAa;65IsAmbiguous IAb(IAa); // expected-error{{call to implicitly-deleted copy constructor}}66 67struct Deleted {68 IsAmbiguous IA; // expected-note{{copy constructor of 'Deleted' is implicitly deleted because field 'IA' has a deleted copy constructor}}69};70Deleted Da;71Deleted Db(Da); // expected-error{{call to implicitly-deleted copy constructor}}72 73// It's implied (but not stated) that this also applies in the case where74// overload resolution would fail.75struct VolatileMember {76 volatile Trivial vm; // expected-note {{has no copy}}77} vm1, vm2(vm1); // expected-error {{deleted}}78 79// -- a direct or virtual base class B that cannot be copied because overload80// resolution results in an ambiguity or a function that is deleted or81// inaccessible82struct AmbiguousCopyBase : Ambiguity { // expected-note 2{{copy constructor of 'AmbiguousCopyBase' is implicitly deleted because base class 'Ambiguity' has multiple copy constructors}}83 NonConst NC;84};85extern AmbiguousCopyBase ACBa;86AmbiguousCopyBase ACBb(ACBa); // expected-error {{deleted copy constructor}}87 88struct DeletedCopyBase : AmbiguousCopyBase {}; // expected-note {{copy constructor of 'DeletedCopyBase' is implicitly deleted because base class 'AmbiguousCopyBase' has a deleted copy constructor}}89extern DeletedCopyBase DCBa;90DeletedCopyBase DCBb(DCBa); // expected-error {{deleted copy constructor}}91 92struct InaccessibleCopyBase : NoAccess {}; // expected-note {{copy constructor of 'InaccessibleCopyBase' is implicitly deleted because base class 'NoAccess' has an inaccessible copy constructor}}93extern InaccessibleCopyBase ICBa;94InaccessibleCopyBase ICBb(ICBa); // expected-error {{deleted copy constructor}}95 96// -- any direct or virtual base class or non-static data member of a type with97// a destructor that is deleted or inaccessible98struct NoAccessDtor {99private:100 ~NoAccessDtor();101 friend struct HasAccessDtor;102};103 104struct HasNoAccessDtor {105 NoAccessDtor NAD; // expected-note{{copy constructor of 'HasNoAccessDtor' is implicitly deleted because field 'NAD' has an inaccessible destructor}}106 HasNoAccessDtor();107 ~HasNoAccessDtor();108};109HasNoAccessDtor HNADa;110HasNoAccessDtor HNADb(HNADa); // expected-error{{call to implicitly-deleted copy constructor}}111 112struct HasAccessDtor {113 NoAccessDtor NAD;114};115HasAccessDtor HADa;116HasAccessDtor HADb(HADa);117 118struct HasNoAccessDtorBase : NoAccessDtor { // expected-note{{copy constructor of 'HasNoAccessDtorBase' is implicitly deleted because base class 'NoAccessDtor' has an inaccessible destructor}}119};120extern HasNoAccessDtorBase HNADBa;121HasNoAccessDtorBase HNADBb(HNADBa); // expected-error{{implicitly-deleted copy constructor}}122 123// -- a non-static data member of rvalue reference type124int some_int;125struct RValue {126 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 &&'}}127};128RValue RVa;129RValue RVb(RVa); // expected-error{{call to implicitly-deleted copy constructor}}130 131// FIXME: The error on the class-name is attached to the location of the132// constructor. This is not especially clear.133struct RValueTmp { // expected-error {{reference member 'ri' binds to a temporary}}134 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}}135};136RValueTmp RVTa; // expected-note {{implicit default constructor for 'RValueTmp' first required here}}137RValueTmp RVTb(RVTa); // expected-error{{call to implicitly-deleted copy constructor}}138 139namespace PR13381 {140 struct S {141 S(const S&);142 S(const volatile S&) = delete; // expected-note{{deleted here}}143 };144 struct T {145 volatile S s; // expected-note{{field 's' has a deleted copy constructor}}146 };147 T &f();148 T t = f(); // expected-error{{call to implicitly-deleted copy constructor}}149}150 151namespace Mutable {152 struct A {153 A(const A &);154 A(A &) = delete; // expected-note {{deleted here}}155 };156 157 struct B {158 A a;159 B(const B &);160 };161 B::B(const B &) = default;162 163 struct C {164 mutable A a; // expected-note {{deleted because field 'a' has a deleted copy constructor}}165 C(const C &);166 };167 C::C(const C &) = default; // expected-error{{would delete}}168}169