Clang Project

clang_source_code/test/SemaCXX/default-assignment-operator.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
5class Base { // expected-warning{{class 'Base' does not declare any constructor to initialize its non-modifiable members}}
6#if __cplusplus <= 199711L
7// expected-error@-2 {{cannot define the implicit copy assignment operator for 'Base', because non-static reference member 'ref' cannot use copy assignment operator}}
8#endif
9
10  int &ref; // expected-note{{reference member 'ref' will never be initialized}}
11#if __cplusplus <= 199711L
12  // expected-note@-2 {{declared here}}
13#else
14  // expected-note@-4 2 {{copy assignment operator of 'Base' is implicitly deleted because field 'ref' is of reference type 'int &'}}
15#endif
16};
17
18class X  : Base {
19#if __cplusplus <= 199711L
20// expected-note@-2 {{assignment operator for 'Base' first required here}}
21// expected-error@-3 {{cannot define the implicit copy assignment operator for 'X', because non-static const member 'cint' cannot use copy assignment operator}}
22#else
23// expected-note@-5 2 {{copy assignment operator of 'X' is implicitly deleted because base class 'Base' has a deleted copy assignment operator}}
24#endif
25
26public: 
27  X();
28  const int cint;
29#if __cplusplus <= 199711L
30// expected-note@-2 {{declared here}}
31#endif
32}; 
33
34struct Y  : X { 
35  Y();
36  Y& operator=(const Y&);
37  Y& operator=(volatile Y&);
38  Y& operator=(const volatile Y&);
39  Y& operator=(Y&);
40}; 
41
42class Z : Y {};
43
44Z z1;
45Z z2;
46
47// Test1
48void f(X x, const X cx) {
49  x = cx;
50#if __cplusplus <= 199711L
51  // expected-note@-2 2{{assignment operator for 'X' first required here}}
52#else
53  // expected-error@-4 {{object of type 'X' cannot be assigned because its copy assignment operator is implicitly deleted}}
54#endif
55
56  x = cx;
57#if __cplusplus >= 201103L
58  // expected-error@-2 {{object of type 'X' cannot be assigned because its copy assignment operator is implicitly deleted}}
59#endif
60  z1 = z2;
61}
62
63// Test2
64class T {};
65T t1;
66T t2;
67
68void g() {
69  t1 = t2;
70}
71
72// Test3
73class V {
74public:
75  V();
76  V &operator = (V &b);
77};
78
79class W : V {};
80W w1, w2;
81
82void h() {
83  w1 = w2;
84}
85
86// Test4
87
88class B1 {
89public:
90  B1();
91  B1 &operator = (B1 b);
92};
93
94class D1 : B1 {};
95D1 d1, d2;
96
97void i() {
98  d1 = d2;
99}
100
101// Test5
102
103class E1 {
104#if __cplusplus <= 199711L
105// expected-error@-2 {{cannot define the implicit copy assignment operator for 'E1', because non-static const member 'a' cannot use copy assignment operator}}
106#endif
107
108public:
109  const int a;
110#if __cplusplus <= 199711L
111// expected-note@-2 {{declared here}}
112#else
113// expected-note@-4 {{copy assignment operator of 'E1' is implicitly deleted because field 'a' is of const-qualified type 'const int'}}
114#endif
115  E1() : a(0) {}
116
117};
118
119E1 e1, e2;
120
121void j() {
122  e1 = e2;
123#if __cplusplus <= 199711L
124  // expected-note@-2 {{assignment operator for 'E1' first required here}}
125#else
126  // expected-error@-4 {{object of type 'E1' cannot be assigned because its copy assignment operator is implicitly deleted}}
127#endif
128}
129
130namespace ProtectedCheck {
131  struct X {
132  protected:
133    X &operator=(const X&);
134#if __cplusplus <= 199711L
135    // expected-note@-2 {{declared protected here}}
136#endif
137  };
138
139  struct Y : public X { };
140
141  void f(Y y) { y = y; }
142
143  struct Z {
144#if __cplusplus <= 199711L
145  // expected-error@-2 {{'operator=' is a protected member of 'ProtectedCheck::X'}}
146#endif
147    X x;
148#if __cplusplus >= 201103L
149    // expected-note@-2 {{copy assignment operator of 'Z' is implicitly deleted because field 'x' has an inaccessible copy assignment operator}}
150#endif
151  };
152
153  void f(Z z) { z = z; }
154#if __cplusplus <= 199711L
155  // expected-note@-2 {{implicit copy assignment operator}}
156#else
157  // expected-error@-4 {{object of type 'ProtectedCheck::Z' cannot be assigned because its copy assignment operator is implicitly deleted}}
158#endif
159}
160
161namespace MultiplePaths {
162  struct X0 { 
163    X0 &operator=(const X0&);
164  };
165
166  struct X1 : public virtual X0 { };
167
168  struct X2 : X0, X1 { }; // expected-warning{{direct base 'MultiplePaths::X0' is inaccessible due to ambiguity:\n    struct MultiplePaths::X2 -> struct MultiplePaths::X0\n    struct MultiplePaths::X2 -> struct MultiplePaths::X1 -> struct MultiplePaths::X0}}
169
170  void f(X2 x2) { x2 = x2; }
171}
172