1 // { dg-do run  }
2 // Copyright (C) 1999 Free Software Foundation, Inc.
3 // Contributed by Nathan Sidwell 20 May 1999 <nathan@acm.org>
4 
5 // Although anon unions cannot have user defined member functions
6 // [class.union/2].  They should have implicitly defined copy ctors and
7 // and the like [class.copy/4].  Make sure we generate one of the correct
8 // signature and that it works ok.
9 
10 extern "C" void abort();
11 
12 struct A
13 {
14   union
15   {
16     int a;
17   };
18 };
19 union B
20 {
21   int a;
22 };
23 
Ctor(A const & src)24 static A Ctor(A const &src)
25 {
26   A result(src);  // this should not cause a const violation
27 
28   result = src;   // and neither should this
29 
30   return result;
31 }
32 
33 typedef __SIZE_TYPE__ size_t;
34 
new(size_t,void * ptr)35 void *operator new(size_t, void *ptr)
36 {
37   return ptr;
38 }
39 
40 // check copy ctor and assignment for plain union
check_union()41 void check_union()
42 {
43   B b1;
44   B b2;
45 
46   b1.a = 5;
47   b2.a = 6;
48   b2 = b1;
49   if(b2.a != 5)
50     abort();
51 
52   b2.a = 6;
53   new (&b2) B(b1);
54   if(b2.a != 5)
55     abort();
56 
57   return;
58 }
59 
60 // check copy ctor and assignment for class containing anon-union
check_union_member()61 void check_union_member()
62 {
63   A a1;
64   A a2;
65 
66   a1.a = 5;
67   a2.a = 6;
68   a2 = a1;
69   if(a2.a != 5)
70     abort();
71 
72   a2.a = 6;
73   new (&a2) A(a1);
74   if(a2.a != 5)
75     abort();
76 
77   return;
78 }
79 
main()80 int main()
81 {
82   check_union();
83   check_union_member();
84 
85   return 0;
86 }
87