xref: /llvm-project/clang/test/CXX/class/class.union/p1.cpp (revision fcd50538c2ec8d2f62edb5a40a94ee354e8e5b42)
153851923SRichard Smith // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
28a273911SDouglas Gregor 
3af6ab889SMike Stump void abort() __attribute__((noreturn));
48a273911SDouglas Gregor 
58a273911SDouglas Gregor class Okay {
68a273911SDouglas Gregor   int a_;
78a273911SDouglas Gregor };
88a273911SDouglas Gregor 
98a273911SDouglas Gregor class Virtual {
109fcede85SJohn McCall   virtual void foo() { abort(); } // expected-note 4 {{because type 'Virtual' has a virtual member function}}
118a273911SDouglas Gregor };
128a273911SDouglas Gregor 
139fcede85SJohn McCall class VirtualBase : virtual Okay { // expected-note 4 {{because type 'VirtualBase' has a virtual base class}}
148a273911SDouglas Gregor };
158a273911SDouglas Gregor 
168a273911SDouglas Gregor class Ctor {
1792f241f1SRichard Smith   Ctor() { abort(); } // expected-note 2{{because type 'Ctor' has a user-provided default constructor}} expected-note 2{{here}}
188a273911SDouglas Gregor };
19e24b1620SSebastian Redl class Ctor2 {
2092f241f1SRichard Smith   Ctor2(); // expected-note {{because type 'Ctor2' has a user-provided default constructor}} expected-note 2{{here}}
21e24b1620SSebastian Redl };
2292f241f1SRichard Smith class CtorTmpl { // expected-note {{because type 'CtorTmpl' has no default constructor}}
2392f241f1SRichard Smith   template<typename T> CtorTmpl(); // expected-note {{implicit default constructor suppressed by user-declared constructor}}
248e1c932fSRichard Smith };
258a273911SDouglas Gregor 
2692f241f1SRichard Smith class CopyCtor { // expected-note 2{{because no constructor can be used to copy an object of type 'const CopyCtor'}}
2792f241f1SRichard Smith   CopyCtor(CopyCtor &cc) { abort(); }
288a273911SDouglas Gregor };
298a273911SDouglas Gregor 
3092f241f1SRichard Smith class CopyAssign { // expected-note 2 {{because no assignment operator can be used to copy an object of type 'const CopyAssign'}}
3192f241f1SRichard Smith   CopyAssign& operator=(CopyAssign& CA) { abort(); }
328a273911SDouglas Gregor };
338a273911SDouglas Gregor 
348a273911SDouglas Gregor class Dtor {
3592f241f1SRichard Smith   ~Dtor() { abort(); } // expected-note 2 {{because type 'Dtor' has a user-provided destructor}} expected-note 2{{here}}
368a273911SDouglas Gregor };
378a273911SDouglas Gregor 
388a273911SDouglas Gregor union U1 {
398a273911SDouglas Gregor   Virtual v; // expected-error {{union member 'v' has a non-trivial copy constructor}}
408a273911SDouglas Gregor   VirtualBase vbase; // expected-error {{union member 'vbase' has a non-trivial copy constructor}}
41*fcd50538SEric Fiselier   Ctor ctor; // expected-error {{union member 'ctor' has a non-trivial default constructor}}
42*fcd50538SEric Fiselier   Ctor2 ctor2; // expected-error {{union member 'ctor2' has a non-trivial default constructor}}
43*fcd50538SEric Fiselier   CtorTmpl ctortmpl; // expected-error {{union member 'ctortmpl' has a non-trivial default constructor}}
448a273911SDouglas Gregor   CopyCtor copyctor; // expected-error {{union member 'copyctor' has a non-trivial copy constructor}}
458a273911SDouglas Gregor   CopyAssign copyassign; // expected-error {{union member 'copyassign' has a non-trivial copy assignment operator}}
468a273911SDouglas Gregor   Dtor dtor; // expected-error {{union member 'dtor' has a non-trivial destructor}}
478a273911SDouglas Gregor   Okay okay;
488a273911SDouglas Gregor };
498a273911SDouglas Gregor 
508a273911SDouglas Gregor union U2 {
518a273911SDouglas Gregor   struct {
5292f241f1SRichard Smith     Virtual v; // expected-note {{because the function selected to copy field of type 'Virtual' is not trivial}}
538a273911SDouglas Gregor   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
548a273911SDouglas Gregor   struct {
5592f241f1SRichard Smith     VirtualBase vbase; // expected-note {{because the function selected to copy field of type 'VirtualBase' is not trivial}}
568a273911SDouglas Gregor   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
578a273911SDouglas Gregor   struct {
5892f241f1SRichard Smith     Ctor ctor; // expected-note {{because field of type 'Ctor' has a user-provided default constructor}}
59*fcd50538SEric Fiselier   } m3; // expected-error {{union member 'm3' has a non-trivial default constructor}}
608a273911SDouglas Gregor   struct {
6192f241f1SRichard Smith     Ctor2 ctor2; // expected-note {{because field of type 'Ctor2' has a user-provided default constructor}}
62*fcd50538SEric Fiselier   } m3a; // expected-error {{union member 'm3a' has a non-trivial default constructor}}
6392f241f1SRichard Smith   struct { // expected-note {{no constructor can be used to copy an object of type 'const}}
6492f241f1SRichard Smith     CopyCtor copyctor;
658a273911SDouglas Gregor   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
6692f241f1SRichard Smith   struct { // expected-note {{no assignment operator can be used to copy an object of type 'const}}
6792f241f1SRichard Smith     CopyAssign copyassign;
688a273911SDouglas Gregor   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
698a273911SDouglas Gregor   struct {
7092f241f1SRichard Smith     Dtor dtor; // expected-note {{because field of type 'Dtor' has a user-provided destructor}}
718a273911SDouglas Gregor   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
728a273911SDouglas Gregor   struct {
738a273911SDouglas Gregor     Okay okay;
748a273911SDouglas Gregor   } m7;
758a273911SDouglas Gregor };
768a273911SDouglas Gregor 
778a273911SDouglas Gregor union U3 {
7892f241f1SRichard Smith   struct s1 : Virtual { // expected-note {{because the function selected to copy base class of type 'Virtual' is not trivial}}
798a273911SDouglas Gregor   } m1; // expected-error {{union member 'm1' has a non-trivial copy constructor}}
8092f241f1SRichard Smith   struct s2 : VirtualBase { // expected-note {{because the function selected to copy base class of type 'VirtualBase' is not trivial}}
818a273911SDouglas Gregor   } m2; // expected-error {{union member 'm2' has a non-trivial copy constructor}}
8292f241f1SRichard Smith   struct s3 : Ctor { // expected-note {{because base class of type 'Ctor' has a user-provided default constructor}}
83*fcd50538SEric Fiselier   } m3; // expected-error {{union member 'm3' has a non-trivial default constructor}}
8492f241f1SRichard Smith   struct s3a : Ctor2 { // expected-note {{because base class of type 'Ctor2' has a user-provided default constructor}}
85*fcd50538SEric Fiselier   } m3a; // expected-error {{union member 'm3a' has a non-trivial default constructor}}
8692f241f1SRichard Smith   struct s4 : CopyCtor { // expected-note {{because no constructor can be used to copy an object of type 'const U3::s4'}}
878a273911SDouglas Gregor   } m4; // expected-error {{union member 'm4' has a non-trivial copy constructor}}
8892f241f1SRichard Smith   struct s5 : CopyAssign { // expected-note {{because no assignment operator can be used to copy an object of type 'const U3::s5'}}
898a273911SDouglas Gregor   } m5; // expected-error {{union member 'm5' has a non-trivial copy assignment operator}}
9092f241f1SRichard Smith   struct s6 : Dtor { // expected-note {{because base class of type 'Dtor' has a user-provided destructor}}
918a273911SDouglas Gregor   } m6; // expected-error {{union member 'm6' has a non-trivial destructor}}
928a273911SDouglas Gregor   struct s7 : Okay {
938a273911SDouglas Gregor   } m7;
9492f241f1SRichard Smith   struct s8 {
9592f241f1SRichard Smith     s8(...) = delete; // expected-note {{because it is a variadic function}} expected-warning {{C++11}}
96*fcd50538SEric Fiselier   } m8; // expected-error {{union member 'm8' has a non-trivial default constructor}}
978a273911SDouglas Gregor };
988a273911SDouglas Gregor 
992ceb347eSAnders Carlsson union U4 {
10042973755SRichard Smith   static int i1; // expected-warning {{static data member 'i1' in union is a C++11 extension}}
1012ceb347eSAnders Carlsson };
10242973755SRichard Smith int U4::i1 = 10;
1032ceb347eSAnders Carlsson 
1042ceb347eSAnders Carlsson union U5 {
1052ceb347eSAnders Carlsson   int& i1; // expected-error {{union member 'i1' has reference type 'int &'}}
1062ceb347eSAnders Carlsson };
1072ceb347eSAnders Carlsson 
108593f993aSRichard Smith union U6 {
109593f993aSRichard Smith   struct S {
110593f993aSRichard Smith     int &i;
111593f993aSRichard Smith   } s; // ok
112593f993aSRichard Smith };
113593f993aSRichard Smith 
1148a273911SDouglas Gregor template <class A, class B> struct Either {
1158a273911SDouglas Gregor   bool tag;
1169fcede85SJohn McCall   union { // expected-note 6 {{in instantiation of member class}}
1178a273911SDouglas Gregor     A a;
1189fcede85SJohn McCall     B b; // expected-error 6 {{non-trivial}}
1198a273911SDouglas Gregor   };
1208a273911SDouglas Gregor 
1219fcede85SJohn McCall   Either(const A& a) : tag(true), a(a) {}
1229fcede85SJohn McCall   Either(const B& b) : tag(false), b(b) {}
1238a273911SDouglas Gregor };
1248a273911SDouglas Gregor 
1258a273911SDouglas Gregor void fred() {
1269fcede85SJohn McCall   Either<int,Virtual> virt(0); // expected-note {{in instantiation of template}}
1279fcede85SJohn McCall   Either<int,VirtualBase> vbase(0); // expected-note {{in instantiation of template}}
1289fcede85SJohn McCall   Either<int,Ctor> ctor(0); // expected-note {{in instantiation of template}}
1299fcede85SJohn McCall   Either<int,CopyCtor> copyctor(0); // expected-note {{in instantiation of template}}
1309fcede85SJohn McCall   Either<int,CopyAssign> copyassign(0); // expected-note {{in instantiation of template}}
1319fcede85SJohn McCall   Either<int,Dtor> dtor(0); // expected-note {{in instantiation of template}}
1328a273911SDouglas Gregor   Either<int,Okay> okay(0);
1338a273911SDouglas Gregor }
134