p1.cpp
12.8 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// RUN: %clang_cc1 -fsyntax-only -verify %s
namespace test0 {
class A {
protected: int x; // expected-note 3 {{declared}} \
// expected-note {{member is declared here}}
static int sx; // expected-note 3 {{declared}} \
// expected-note {{member is declared here}}
};
class B : public A {
};
class C : protected A { // expected-note {{declared}}
};
class D : private B { // expected-note 3 {{constrained}}
};
void test(A &a) {
(void) a.x; // expected-error {{'x' is a protected member}}
(void) a.sx; // expected-error {{'sx' is a protected member}}
}
void test(B &b) {
(void) b.x; // expected-error {{'x' is a protected member}}
(void) b.sx; // expected-error {{'sx' is a protected member}}
}
void test(C &c) {
(void) c.x; // expected-error {{'x' is a protected member}} expected-error {{protected base class}}
(void) c.sx; // expected-error {{'sx' is a protected member}}
}
void test(D &d) {
(void) d.x; // expected-error {{'x' is a private member}} expected-error {{private base class}}
(void) d.sx; // expected-error {{'sx' is a private member}}
}
}
namespace test1 {
class A {
protected: int x;
static int sx;
static void test(A&);
};
class B : public A {
static void test(B&);
};
class C : protected A {
static void test(C&);
};
class D : private B {
static void test(D&);
};
void A::test(A &a) {
(void) a.x;
(void) a.sx;
}
void B::test(B &b) {
(void) b.x;
(void) b.sx;
}
void C::test(C &c) {
(void) c.x;
(void) c.sx;
}
void D::test(D &d) {
(void) d.x;
(void) d.sx;
}
}
namespace test2 {
class A {
protected: int x; // expected-note 3 {{can only access this member on an object of type}}
static int sx;
static void test(A&);
};
class B : public A {
static void test(A&);
};
class C : protected A {
static void test(A&);
};
class D : private B {
static void test(A&);
};
void A::test(A &a) {
(void) a.x;
(void) a.sx;
}
void B::test(A &a) {
(void) a.x; // expected-error {{'x' is a protected member}}
(void) a.sx;
}
void C::test(A &a) {
(void) a.x; // expected-error {{'x' is a protected member}}
(void) a.sx;
}
void D::test(A &a) {
(void) a.x; // expected-error {{'x' is a protected member}}
(void) a.sx;
}
}
namespace test3 {
class B;
class A {
protected: int x; //expected-note {{declared protected}} // expected-note {{can only access this member on an object of type}}
static int sx;
static void test(B&);
};
class B : public A {
static void test(B&);
};
class C : protected A {
static void test(B&);
};
class D : private B {
static void test(B&);
};
void A::test(B &b) {
(void) b.x;
(void) b.sx;
}
void B::test(B &b) {
(void) b.x;
(void) b.sx;
}
void C::test(B &b) {
(void) b.x; // expected-error {{'x' is a protected member}}
(void) b.sx;
}
void D::test(B &b) {
(void) b.x; // expected-error {{'x' is a protected member}}
(void) b.sx;
}
}
namespace test4 {
class C;
class A {
protected: int x; // expected-note 2{{declared protected here}} expected-note{{member is declared here}}
static int sx; // expected-note 3{{member is declared here}}
static void test(C&);
};
class B : public A {
static void test(C&);
};
class C : protected A { // expected-note 4 {{constrained}} expected-note 3 {{declared}}
static void test(C&);
};
class D : private B {
static void test(C&);
};
void A::test(C &c) {
(void) c.x; // expected-error {{'x' is a protected member}} \
// expected-error {{protected base class}}
(void) c.sx; // expected-error {{'sx' is a protected member}}
}
void B::test(C &c) {
(void) c.x; // expected-error {{'x' is a protected member}} \
// expected-error {{protected base class}}
(void) c.sx; // expected-error {{'sx' is a protected member}}
}
void C::test(C &c) {
(void) c.x;
(void) c.sx;
}
void D::test(C &c) {
(void) c.x; // expected-error {{'x' is a protected member}} \
// expected-error {{protected base class}}
(void) c.sx; // expected-error {{'sx' is a protected member}}
}
}
namespace test5 {
class D;
class A {
protected: int x; // expected-note 3{{member is declared here}}
static int sx; // expected-note 3{{member is declared here}}
static void test(D&);
};
class B : public A {
static void test(D&);
};
class C : protected A {
static void test(D&);
};
class D : private B { // expected-note 9 {{constrained}}
static void test(D&);
};
void A::test(D &d) {
(void) d.x; // expected-error {{'x' is a private member}} \
// expected-error {{cannot cast}}
(void) d.sx; // expected-error {{'sx' is a private member}}
}
void B::test(D &d) {
(void) d.x; // expected-error {{'x' is a private member}} \
// expected-error {{cannot cast}}
(void) d.sx; // expected-error {{'sx' is a private member}}
}
void C::test(D &d) {
(void) d.x; // expected-error {{'x' is a private member}} \
// expected-error {{cannot cast}}
(void) d.sx; // expected-error {{'sx' is a private member}}
}
void D::test(D &d) {
(void) d.x;
(void) d.sx;
}
}
namespace test6 {
class Static {};
class A {
protected:
void foo(int); // expected-note 3 {{can only access this member on an object of type}}
void foo(long);
static void foo(Static);
static void test(A&);
};
class B : public A {
static void test(A&);
};
class C : protected A {
static void test(A&);
};
class D : private B {
static void test(A&);
};
void A::test(A &a) {
a.foo(10);
a.foo(Static());
}
void B::test(A &a) {
a.foo(10); // expected-error {{'foo' is a protected member}}
a.foo(Static());
}
void C::test(A &a) {
a.foo(10); // expected-error {{'foo' is a protected member}}
a.foo(Static());
}
void D::test(A &a) {
a.foo(10); // expected-error {{'foo' is a protected member}}
a.foo(Static());
}
}
namespace test7 {
class Static {};
class A {
protected:
void foo(int); // expected-note 3 {{must name member using the type of the current context}}
void foo(long);
static void foo(Static);
static void test();
};
class B : public A {
static void test();
};
class C : protected A {
static void test();
};
class D : private B {
static void test();
};
void A::test() {
void (A::*x)(int) = &A::foo;
void (*sx)(Static) = &A::foo;
}
void B::test() {
void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
void (*sx)(Static) = &A::foo;
}
void C::test() {
void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
void (*sx)(Static) = &A::foo;
}
void D::test() {
void (A::*x)(int) = &A::foo; // expected-error {{'foo' is a protected member}}
void (*sx)(Static) = &A::foo;
}
}
namespace test8 {
class Static {};
class A {
protected:
void foo(int); // expected-note 3 {{must name member using the type of the current context}}
void foo(long);
static void foo(Static);
static void test();
};
class B : public A {
static void test();
};
class C : protected A {
static void test();
};
class D : private B {
static void test();
};
void call(void (A::*)(int));
void calls(void (*)(Static));
void A::test() {
call(&A::foo);
calls(&A::foo);
}
void B::test() {
call(&A::foo); // expected-error {{'foo' is a protected member}}
calls(&A::foo);
}
void C::test() {
call(&A::foo); // expected-error {{'foo' is a protected member}}
calls(&A::foo);
}
void D::test() {
call(&A::foo); // expected-error {{'foo' is a protected member}}
calls(&A::foo);
}
}
namespace test9 {
class A { // expected-note {{member is declared here}}
protected: int foo(); // expected-note 4 {{declared}} expected-note 3 {{can only access this member on an object of type}} expected-note 2 {{member is declared here}}
};
class B : public A { // expected-note {{member is declared here}}
friend class D;
};
class C : protected B { // expected-note {{declared}} \
// expected-note 9 {{constrained}}
};
class D : public A {
static void test(A &a) {
a.foo(); // expected-error {{'foo' is a protected member}}
a.A::foo(); // expected-error {{'foo' is a protected member}}
a.B::foo(); // expected-error {{'foo' is a protected member}}
a.C::foo(); // expected-error {{'foo' is a protected member}}
a.D::foo(); // expected-error {{'foo' is a protected member}}
}
static void test(B &b) {
b.foo();
b.A::foo();
b.B::foo(); // accessible as named in A
b.C::foo(); // expected-error {{'foo' is a protected member}}
}
static void test(C &c) {
c.foo(); // expected-error {{'foo' is a protected member}} \
// expected-error {{cannot cast}}
c.A::foo(); // expected-error {{'A' is a protected member}} \
// expected-error {{cannot cast}}
c.B::foo(); // expected-error {{'B' is a protected member}} \
// expected-error {{cannot cast}}
c.C::foo(); // expected-error {{'foo' is a protected member}} \
// expected-error {{cannot cast}}
}
static void test(D &d) {
d.foo();
d.A::foo();
d.B::foo();
d.C::foo(); // expected-error {{'foo' is a protected member}}
}
};
}
namespace test10 {
template<typename T> class A {
protected:
int foo();
int foo() const;
~A() { foo(); }
};
template class A<int>;
}
// rdar://problem/8360285: class.protected friendship
namespace test11 {
class A {
protected:
int foo();
};
class B : public A {
friend class C;
};
class C {
void test() {
B b;
b.A::foo();
}
};
}
// This friendship is considered because a public member of A would be
// a private member of C.
namespace test12 {
class A { protected: int foo(); };
class B : public virtual A {};
class C : private B { friend void test(); };
class D : private C, public virtual A {};
void test() {
D d;
d.A::foo();
}
}
// This friendship is not considered because a public member of A is
// inaccessible in C.
namespace test13 {
class A { protected: int foo(); }; // expected-note {{declared protected here}}
class B : private virtual A {};
class C : private B { friend void test(); };
class D : public virtual A {};
void test() {
D d;
d.A::foo(); // expected-error {{protected member}}
}
}
// PR8058
namespace test14 {
class A {
protected:
template <class T> void temp(T t); // expected-note {{must name member using the type of the current context}}
void nontemp(int); // expected-note {{must name member using the type of the current context}}
template <class T> void ovl_temp(T t); // expected-note {{must name member using the type of the current context}}
void ovl_temp(float);
void ovl_nontemp(int); // expected-note {{must name member using the type of the current context}}
void ovl_nontemp(float);
template <class T> void ovl_withtemp(T);
void ovl_withtemp(int); // expected-note {{must name member using the type of the current context}}
};
class B : public A {
void use() {
void (A::*ptr)(int);
ptr = &A::temp; // expected-error {{protected member}}
ptr = &A::nontemp; // expected-error {{protected member}}
ptr = &A::ovl_temp; // expected-error {{protected member}}
ptr = &A::ovl_nontemp; // expected-error {{protected member}}
ptr = &A::ovl_withtemp; // expected-error {{protected member}}
}
};
}
namespace test15 {
class A {
protected:
A(); // expected-note 2 {{protected constructor can only be used to construct a base class subobject}}
A(const A &); // expected-note {{protected constructor can only be used to construct a base class subobject}}
~A(); // expected-note 3 {{protected destructor can only be used to destroy a base class subobject}}
};
class B : public A {
// The uses here are fine.
B() {}
B(int i) : A() {}
~B() {}
// All these uses are bad.
void test0() {
A a; // expected-error {{protected constructor}} expected-error {{protected destructor}}
}
A *test1() {
return new A(); // expected-error {{protected constructor}}
}
void test2(A *a) {
delete a; // expected-error {{protected destructor}}
}
A test3(A *a) {
return *a; // expected-error {{protected constructor}}
}
void test4(A *a) {
a->~A(); // expected-error {{protected member}}
}
};
}
namespace test16 {
class A {
protected:
~A();
};
class B : public virtual A {
public:
~B() {}
};
class C : public B {
~C() {}
};
}