friend.cpp
3.88 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
// RUN: %clang_cc1 -fsyntax-only -verify %s
template<typename T> struct A {
struct B { };
friend struct B;
};
void f() {
A<int>::B b;
}
struct C0 {
friend struct A<int>;
};
namespace PR6770 {
namespace N {
int f1(int);
}
using namespace N;
namespace M {
float f1(float);
}
using M::f1;
template<typename T> void f1(T, T);
template <class T>
void f() {
friend class f; // expected-error{{'friend' used outside of class}}
friend class f1; // expected-error{{'friend' used outside of class}}
}
}
namespace friend_redecl_inline {
// We had a bug where instantiating the foo friend declaration would check the
// defined-ness of the most recent decl while checking if the canonical decl was
// inlined.
void foo();
void bar();
template <typename T>
class C {
friend void foo();
friend inline void bar();
};
inline void foo() {}
inline void bar() {}
C<int> c;
}
namespace qualified_friend {
void f(int); // expected-note 2{{type mismatch at 1st parameter}}
template<typename T> void f(T*); // expected-note 2{{could not match 'type-parameter-0-0 *' against 'double'}}
template<typename T> void nondep();
template<typename> struct X1 {
friend void qualified_friend::f(double); // expected-error {{friend declaration of 'f' does not match any declaration in namespace 'qualified_friend'}}
friend void qualified_friend::g(); // expected-error {{friend declaration of 'g' does not match any declaration in namespace 'qualified_friend'}}
};
template<typename T> struct X2 {
friend void qualified_friend::f(T); // expected-error {{friend declaration of 'f' does not match any declaration in namespace 'qualified_friend'}}
};
X1<int> xi;
X2<double> xd; // expected-note {{in instantiation of}}
X2<int> x2i;
struct Y {
void f(int); // expected-note 2{{type mismatch at 1st parameter}}
template<typename T> void f(T*); // expected-note 2{{could not match 'type-parameter-0-0 *' against 'double'}}
template<typename T> void nondep();
};
template<typename> struct Z1 {
friend void Y::f(double); // expected-error {{friend declaration of 'f' does not match any declaration in 'qualified_friend::Y'}}
friend void Y::g(); // expected-error {{friend declaration of 'g' does not match any declaration in 'qualified_friend::Y'}}
};
template<typename T> struct Z2 {
friend void Y::f(T); // expected-error {{friend declaration of 'f' does not match any declaration in 'qualified_friend::Y'}}
};
Z1<int> zi;
Z2<double> zd; // expected-note {{in instantiation of}}
Z2<int> z2i;
template<typename T>
struct OK {
friend void qualified_friend::f(int);
friend void qualified_friend::f(int*);
friend void qualified_friend::f(T*);
friend void qualified_friend::f<T>(T*);
friend void qualified_friend::nondep<int>();
friend void qualified_friend::nondep<T>();
friend void Y::f(int);
friend void Y::f(int*);
friend void Y::f(T*);
friend void Y::f<T>(T*);
friend void Y::nondep<int>();
friend void Y::nondep<T>();
};
OK<float> ok;
}
namespace qualified_friend_finds_nothing {
// FIXME: The status of this example is unclear. For now, we diagnose if the
// qualified declaration has nothing it can redeclare, but allow qualified
// lookup to find later-declared function templates during instantiation.
//
// This matches the behavior of GCC, EDG, ICC, and MSVC (except that GCC and
// ICC bizarrely accept the instantiation of B<float>).
namespace N {}
template<typename T> struct A {
friend void N::f(T); // expected-error {{friend declaration of 'f' does not match}}
};
namespace N { void f(); } // expected-note {{different number of parameters}}
template<typename T> struct B {
friend void N::f(T); // expected-error {{friend declaration of 'f' does not match}}
};
B<float> bf; // expected-note {{in instantiation of}}
namespace N { void f(int); }
B<int> bi; // ok?!
}