Home | History | Annotate | Download | only in class.copy
      1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
      2 
      3 struct Trivial {};
      4 struct 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 constructor
     11 union 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 };
     15 DeletedNTVariant DVa;
     16 DeletedNTVariant DVb(DVa); // expected-error{{call to implicitly-deleted copy constructor}}
     17 
     18 struct 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 };
     24 DeletedNTVariant2 DV2a;
     25 DeletedNTVariant2 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 be
     28 //    copied because overload resolution results in an ambiguity or a function
     29 //    that is deleted or inaccessible
     30 struct NoAccess {
     31   NoAccess() = default;
     32 private:
     33   NoAccess(const NoAccess&);
     34 
     35   friend struct HasAccess;
     36 };
     37 
     38 struct HasNoAccess {
     39   NoAccess NA; // expected-note{{copy constructor of 'HasNoAccess' is implicitly deleted because field 'NA' has an inaccessible copy constructor}}
     40 };
     41 HasNoAccess HNAa;
     42 HasNoAccess HNAb(HNAa); // expected-error{{call to implicitly-deleted copy constructor}}
     43 
     44 struct HasAccess {
     45   NoAccess NA;
     46 };
     47 
     48 HasAccess HAa;
     49 HasAccess HAb(HAa);
     50 
     51 struct NonConst {
     52   NonConst(NonConst&);
     53 };
     54 struct Ambiguity {
     55   Ambiguity(const Ambiguity&);
     56   Ambiguity(volatile Ambiguity&);
     57 };
     58 
     59 struct 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 };
     64 IsAmbiguous IAa;
     65 IsAmbiguous IAb(IAa); // expected-error{{call to implicitly-deleted copy constructor}}
     66 
     67 struct Deleted {
     68   IsAmbiguous IA; // expected-note{{copy constructor of 'Deleted' is implicitly deleted because field 'IA' has a deleted copy constructor}}
     69 };
     70 Deleted Da;
     71 Deleted 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 where
     74 // overload resolution would fail.
     75 struct 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 overload
     80 //    resolution results in an ambiguity or a function that is deleted or
     81 //    inaccessible
     82 struct AmbiguousCopyBase : Ambiguity { // expected-note 2{{copy constructor of 'AmbiguousCopyBase' is implicitly deleted because base class 'Ambiguity' has multiple copy constructors}}
     83   NonConst NC;
     84 };
     85 extern AmbiguousCopyBase ACBa;
     86 AmbiguousCopyBase ACBb(ACBa); // expected-error {{deleted copy constructor}}
     87 
     88 struct DeletedCopyBase : AmbiguousCopyBase {}; // expected-note {{copy constructor of 'DeletedCopyBase' is implicitly deleted because base class 'AmbiguousCopyBase' has a deleted copy constructor}}
     89 extern DeletedCopyBase DCBa;
     90 DeletedCopyBase DCBb(DCBa); // expected-error {{deleted copy constructor}}
     91 
     92 struct InaccessibleCopyBase : NoAccess {}; // expected-note {{copy constructor of 'InaccessibleCopyBase' is implicitly deleted because base class 'NoAccess' has an inaccessible copy constructor}}
     93 extern InaccessibleCopyBase ICBa;
     94 InaccessibleCopyBase ICBb(ICBa); // expected-error {{deleted copy constructor}}
     95 
     96 // -- any direct or virtual base class or non-static data member of a type with
     97 //    a destructor that is deleted or inaccessible
     98 struct NoAccessDtor {
     99 private:
    100   ~NoAccessDtor();
    101   friend struct HasAccessDtor;
    102 };
    103 
    104 struct 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 };
    109 HasNoAccessDtor HNADa;
    110 HasNoAccessDtor HNADb(HNADa); // expected-error{{call to implicitly-deleted copy constructor}}
    111 
    112 struct HasAccessDtor {
    113   NoAccessDtor NAD;
    114 };
    115 HasAccessDtor HADa;
    116 HasAccessDtor HADb(HADa);
    117 
    118 struct HasNoAccessDtorBase : NoAccessDtor { // expected-note{{copy constructor of 'HasNoAccessDtorBase' is implicitly deleted because base class 'NoAccessDtor' has an inaccessible destructor}}
    119 };
    120 extern HasNoAccessDtorBase HNADBa;
    121 HasNoAccessDtorBase HNADBb(HNADBa); // expected-error{{implicitly-deleted copy constructor}}
    122 
    123 // -- a non-static data member of rvalue reference type
    124 struct RValue {
    125   int && ri = 1; // expected-note{{copy constructor of 'RValue' is implicitly deleted because field 'ri' is of rvalue reference type 'int &&'}}
    126   // expected-warning@-1{{binding reference member 'ri' to a temporary}} expected-note@-1 {{here}}
    127 };
    128 RValue RVa;
    129 RValue RVb(RVa); // expected-error{{call to implicitly-deleted copy constructor}}
    130 
    131 namespace PR13381 {
    132   struct S {
    133     S(const S&);
    134     S(const volatile S&) = delete; // expected-note{{deleted here}}
    135   };
    136   struct T {
    137     volatile S s; // expected-note{{field 's' has a deleted copy constructor}}
    138   };
    139   T &f();
    140   T t = f(); // expected-error{{call to implicitly-deleted copy constructor}}
    141 }
    142 
    143 namespace Mutable {
    144   struct A {
    145     A(const A &);
    146     A(A &) = delete; // expected-note {{deleted here}}
    147   };
    148 
    149   struct B {
    150     A a;
    151     B(const B &);
    152   };
    153   B::B(const B &) = default;
    154 
    155   struct C {
    156     mutable A a; // expected-note {{deleted because field 'a' has a deleted copy constructor}}
    157     C(const C &);
    158   };
    159   C::C(const C &) = default; // expected-error{{would delete}}
    160 }
    161