Home | History | Annotate | Download | only in class.copy
      1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
      2 
      3 struct NonTrivial {
      4   NonTrivial(const NonTrivial&);
      5 };
      6 
      7 // A defaulted copy constructor for a class X is defined as deleted if X has:
      8 
      9 // -- a variant member with a non-trivial corresponding constructor
     10 union DeletedNTVariant {
     11   NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}
     12   DeletedNTVariant();
     13 };
     14 DeletedNTVariant DVa;
     15 DeletedNTVariant DVb(DVa); // expected-error{{call to implicitly-deleted copy constructor}}
     16 
     17 struct DeletedNTVariant2 {
     18   union {
     19     NonTrivial NT; // expected-note{{copy constructor of 'DeletedNTVariant2' is implicitly deleted because variant field 'NT' has a non-trivial copy constructor}}
     20   };
     21   DeletedNTVariant2();
     22 };
     23 DeletedNTVariant2 DV2a;
     24 DeletedNTVariant2 DV2b(DV2a); // expected-error{{call to implicitly-deleted copy constructor}}
     25 
     26 // -- a non-static data member of class type M (or array thereof) that cannot be
     27 //    copied because overload resolution results in an ambiguity or a function
     28 //    that is deleted or inaccessible
     29 struct NoAccess {
     30   NoAccess() = default;
     31 private:
     32   NoAccess(const NoAccess&);
     33 
     34   friend struct HasAccess;
     35 };
     36 
     37 struct HasNoAccess {
     38   NoAccess NA; // expected-note{{copy constructor of 'HasNoAccess' is implicitly deleted because field 'NA' has an inaccessible copy constructor}}
     39 };
     40 HasNoAccess HNAa;
     41 HasNoAccess HNAb(HNAa); // expected-error{{call to implicitly-deleted copy constructor}}
     42 
     43 struct HasAccess {
     44   NoAccess NA;
     45 };
     46 
     47 HasAccess HAa;
     48 HasAccess HAb(HAa);
     49 
     50 struct NonConst {
     51   NonConst(NonConst&);
     52 };
     53 struct Ambiguity {
     54   Ambiguity(const Ambiguity&);
     55   Ambiguity(volatile Ambiguity&);
     56 };
     57 
     58 struct IsAmbiguous {
     59   NonConst NC;
     60   Ambiguity A; // expected-note 2{{copy constructor of 'IsAmbiguous' is implicitly deleted because field 'A' has multiple copy constructors}}
     61   IsAmbiguous();
     62 };
     63 IsAmbiguous IAa;
     64 IsAmbiguous IAb(IAa); // expected-error{{call to implicitly-deleted copy constructor}}
     65 
     66 struct Deleted {
     67   IsAmbiguous IA; // expected-note{{copy constructor of 'Deleted' is implicitly deleted because field 'IA' has a deleted copy constructor}}
     68 };
     69 Deleted Da;
     70 Deleted Db(Da); // expected-error{{call to implicitly-deleted copy constructor}}
     71 
     72 // -- a direct or virtual base class B that cannot be copied because overload
     73 //    resolution results in an ambiguity or a function that is deleted or
     74 //    inaccessible
     75 struct AmbiguousCopyBase : Ambiguity { // expected-note 2{{copy constructor of 'AmbiguousCopyBase' is implicitly deleted because base class 'Ambiguity' has multiple copy constructors}}
     76   NonConst NC;
     77 };
     78 extern AmbiguousCopyBase ACBa;
     79 AmbiguousCopyBase ACBb(ACBa); // expected-error {{deleted copy constructor}}
     80 
     81 struct DeletedCopyBase : AmbiguousCopyBase {}; // expected-note {{copy constructor of 'DeletedCopyBase' is implicitly deleted because base class 'AmbiguousCopyBase' has a deleted copy constructor}}
     82 extern DeletedCopyBase DCBa;
     83 DeletedCopyBase DCBb(DCBa); // expected-error {{deleted copy constructor}}
     84 
     85 struct InaccessibleCopyBase : NoAccess {}; // expected-note {{copy constructor of 'InaccessibleCopyBase' is implicitly deleted because base class 'NoAccess' has an inaccessible copy constructor}}
     86 extern InaccessibleCopyBase ICBa;
     87 InaccessibleCopyBase ICBb(ICBa); // expected-error {{deleted copy constructor}}
     88 
     89 // -- any direct or virtual base class or non-static data member of a type with
     90 //    a destructor that is deleted or inaccessible
     91 struct NoAccessDtor {
     92 private:
     93   ~NoAccessDtor();
     94   friend struct HasAccessDtor;
     95 };
     96 
     97 struct HasNoAccessDtor {
     98   NoAccessDtor NAD; // expected-note{{copy constructor of 'HasNoAccessDtor' is implicitly deleted because field 'NAD' has an inaccessible destructor}}
     99   HasNoAccessDtor();
    100   ~HasNoAccessDtor();
    101 };
    102 HasNoAccessDtor HNADa;
    103 HasNoAccessDtor HNADb(HNADa); // expected-error{{call to implicitly-deleted copy constructor}}
    104 
    105 struct HasAccessDtor {
    106   NoAccessDtor NAD;
    107 };
    108 HasAccessDtor HADa;
    109 HasAccessDtor HADb(HADa);
    110 
    111 struct HasNoAccessDtorBase : NoAccessDtor { // expected-note{{copy constructor of 'HasNoAccessDtorBase' is implicitly deleted because base class 'NoAccessDtor' has an inaccessible destructor}}
    112 };
    113 extern HasNoAccessDtorBase HNADBa;
    114 HasNoAccessDtorBase HNADBb(HNADBa); // expected-error{{implicitly-deleted copy constructor}}
    115 
    116 // -- a non-static data member of rvalue reference type
    117 struct RValue {
    118   int && ri = 1; // expected-note{{copy constructor of 'RValue' is implicitly deleted because field 'ri' is of rvalue reference type 'int &&'}}
    119   // expected-warning@-1{{binding reference member 'ri' to a temporary}} expected-note@-1 {{here}}
    120 };
    121 RValue RVa;
    122 RValue RVb(RVa); // expected-error{{call to implicitly-deleted copy constructor}}
    123 
    124 namespace PR13381 {
    125   struct S {
    126     S(const S&);
    127     S(const volatile S&) = delete; // expected-note{{deleted here}}
    128   };
    129   struct T {
    130     volatile S s; // expected-note{{field 's' has a deleted copy constructor}}
    131   };
    132   T &f();
    133   T t = f(); // expected-error{{call to implicitly-deleted copy constructor}}
    134 }
    135