summaryrefslogtreecommitdiff
path: root/test/SemaCXX/default-assignment-operator.cpp
blob: 57cb77d6cb461161eee368bc81476e3da12038b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s

class Base { // expected-warning{{class 'Base' does not declare any constructor to initialize its non-modifiable members}}
#if __cplusplus <= 199711L
// expected-error@-2 {{cannot define the implicit copy assignment operator for 'Base', because non-static reference member 'ref' cannot use copy assignment operator}}
#endif

  int &ref; // expected-note{{reference member 'ref' will never be initialized}}
#if __cplusplus <= 199711L
  // expected-note@-2 {{declared here}}
#else
  // expected-note@-4 2 {{copy assignment operator of 'Base' is implicitly deleted because field 'ref' is of reference type 'int &'}}
#endif
};

class X  : Base {
#if __cplusplus <= 199711L
// expected-note@-2 {{assignment operator for 'Base' first required here}}
// expected-error@-3 {{cannot define the implicit copy assignment operator for 'X', because non-static const member 'cint' cannot use copy assignment operator}}
#else
// expected-note@-5 2 {{copy assignment operator of 'X' is implicitly deleted because base class 'Base' has a deleted copy assignment operator}}
#endif

public: 
  X();
  const int cint;
#if __cplusplus <= 199711L
// expected-note@-2 {{declared here}}
#endif
}; 

struct Y  : X { 
  Y();
  Y& operator=(const Y&);
  Y& operator=(volatile Y&);
  Y& operator=(const volatile Y&);
  Y& operator=(Y&);
}; 

class Z : Y {};

Z z1;
Z z2;

// Test1
void f(X x, const X cx) {
  x = cx;
#if __cplusplus <= 199711L
  // expected-note@-2 2{{assignment operator for 'X' first required here}}
#else
  // expected-error@-4 {{object of type 'X' cannot be assigned because its copy assignment operator is implicitly deleted}}
#endif

  x = cx;
#if __cplusplus >= 201103L
  // expected-error@-2 {{object of type 'X' cannot be assigned because its copy assignment operator is implicitly deleted}}
#endif
  z1 = z2;
}

// Test2
class T {};
T t1;
T t2;

void g() {
  t1 = t2;
}

// Test3
class V {
public:
  V();
  V &operator = (V &b);
};

class W : V {};
W w1, w2;

void h() {
  w1 = w2;
}

// Test4

class B1 {
public:
  B1();
  B1 &operator = (B1 b);
};

class D1 : B1 {};
D1 d1, d2;

void i() {
  d1 = d2;
}

// Test5

class E1 {
#if __cplusplus <= 199711L
// expected-error@-2 {{cannot define the implicit copy assignment operator for 'E1', because non-static const member 'a' cannot use copy assignment operator}}
#endif

public:
  const int a;
#if __cplusplus <= 199711L
// expected-note@-2 {{declared here}}
#else
// expected-note@-4 {{copy assignment operator of 'E1' is implicitly deleted because field 'a' is of const-qualified type 'const int'}}
#endif
  E1() : a(0) {}

};

E1 e1, e2;

void j() {
  e1 = e2;
#if __cplusplus <= 199711L
  // expected-note@-2 {{assignment operator for 'E1' first required here}}
#else
  // expected-error@-4 {{object of type 'E1' cannot be assigned because its copy assignment operator is implicitly deleted}}
#endif
}

namespace ProtectedCheck {
  struct X {
  protected:
    X &operator=(const X&);
#if __cplusplus <= 199711L
    // expected-note@-2 {{declared protected here}}
#endif
  };

  struct Y : public X { };

  void f(Y y) { y = y; }

  struct Z {
#if __cplusplus <= 199711L
  // expected-error@-2 {{'operator=' is a protected member of 'ProtectedCheck::X'}}
#endif
    X x;
#if __cplusplus >= 201103L
    // expected-note@-2 {{copy assignment operator of 'Z' is implicitly deleted because field 'x' has an inaccessible copy assignment operator}}
#endif
  };

  void f(Z z) { z = z; }
#if __cplusplus <= 199711L
  // expected-note@-2 {{implicit copy assignment operator}}
#else
  // expected-error@-4 {{object of type 'ProtectedCheck::Z' cannot be assigned because its copy assignment operator is implicitly deleted}}
#endif
}

namespace MultiplePaths {
  struct X0 { 
    X0 &operator=(const X0&);
  };

  struct X1 : public virtual X0 { };

  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}}

  void f(X2 x2) { x2 = x2; }
}