blob: d1d4b628ba2d98492d30d9be6e7a436f8e5109dc [file] [log] [blame]
Serge Pavlov06b7a872016-10-04 10:11:43 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
2
3// If a friend function is defined in several non-template classes,
4// it is an error.
5
6void func1(int);
7struct C1a {
8 friend void func1(int) {} // expected-note{{previous definition is here}}
9};
10struct C1b {
11 friend void func1(int) {} // expected-error{{redefinition of 'func1'}}
12};
13
14
15// If a friend function is defined in both non-template and template
16// classes it is an error only if the template is instantiated.
17
18void func2(int);
19struct C2a {
20 friend void func2(int) {}
21};
22template<typename T> struct C2b {
23 friend void func2(int) {}
24};
25
26void func3(int);
27struct C3a {
28 friend void func3(int) {} // expected-note{{previous definition is here}}
29};
30template<typename T> struct C3b {
31 friend void func3(int) {} // expected-error{{redefinition of 'func3'}}
32};
33C3b<long> c3; // expected-note{{in instantiation of template class 'C3b<long>' requested here}}
34
35
36// If a friend function is defined in several template classes it is an error
37// only if several templates are instantiated.
38
39void func4(int);
40template<typename T> struct C4a {
41 friend void func4(int) {}
42};
43template<typename T> struct C4b {
44 friend void func4(int) {}
45};
46
47
48void func5(int);
49template<typename T> struct C5a {
50 friend void func5(int) {}
51};
52template<typename T> struct C5b {
53 friend void func5(int) {}
54};
55C5a<long> c5a;
56
57void func6(int);
58template<typename T> struct C6a {
59 friend void func6(int) {} // expected-note{{previous definition is here}}
60};
61template<typename T> struct C6b {
62 friend void func6(int) {} // expected-error{{redefinition of 'func6'}}
63};
64C6a<long> c6a;
65C6b<int*> c6b; // expected-note{{in instantiation of template class 'C6b<int *>' requested here}}
66
67void func7(int);
68template<typename T> struct C7 {
69 friend void func7(int) {} // expected-error{{redefinition of 'func7'}}
70 // expected-note@-1{{previous definition is here}}
71};
72C7<long> c7a;
73C7<int*> c7b; // expected-note{{in instantiation of template class 'C7<int *>' requested here}}
74
75
76// Even if clases are not instantiated and hence friend functions defined in them are not
77// available, their declarations can be checked.
78
79void func8(int); // expected-note{{previous declaration is here}}
80template<typename T> struct C8a {
81 friend long func8(int); // expected-error{{functions that differ only in their return type cannot be overloaded}}
82};
83
84void func9(int); // expected-note{{previous declaration is here}}
85template<typename T> struct C9a {
86 friend int func9(int); // expected-error{{functions that differ only in their return type cannot be overloaded}}
87};
88
89void func10(int); // expected-note{{previous declaration is here}}
90template<typename T> struct C10a {
91 friend int func10(int); // expected-error{{functions that differ only in their return type cannot be overloaded}}
92};
93
94void func_11(); // expected-note{{previous declaration is here}}
95template<typename T> class C11 {
96 friend int func_11(); // expected-error{{functions that differ only in their return type cannot be overloaded}}
97};
98
99void func_12(int x); // expected-note{{previous declaration is here}}
100template<typename T> class C12 {
101 friend void func_12(int x = 0); // expected-error{{friend declaration specifying a default argument must be the only declaration}}
102};
103
104
105namespace pr22307 {
106
107struct t {
108 friend int leak(t);
109};
110
111template<typename v>
112struct m {
113 friend int leak(t) { return sizeof(v); } // expected-error{{redefinition of 'leak'}} expected-note{{previous definition is here}}
114};
115
116template struct m<char>;
117template struct m<short>; // expected-note{{in instantiation of template class 'pr22307::m<short>' requested here}}
118
119int main() {
120 leak(t());
121}
122
123}
124
125namespace pr17923 {
126
127void f(unsigned long long);
128
129template<typename T> struct X {
130 friend void f(unsigned long long) {
131 T t;
132 }
133};
134
135int main() { f(1234); }
136
137}
138
139namespace pr17923a {
140
141int get();
142
143template< int value >
144class set {
145 friend int get()
146 { return value; } // return 0; is OK
147};
148
149template class set< 5 >;
150
151int main() {
152 get();
153}
154
155}
156
157namespace pr8035 {
158
159void Function();
160
161int main(int argc, char* argv[]) {
162 Function();
163}
164
165template <typename T>
166struct Test {
167 friend void Function() { }
168};
169
170template class Test<int>;
171
172}
Serge Pavlov25dbe1a2017-06-21 12:46:57 +0000173
174namespace pr14785 {
175template<typename T>
176struct Somewhat {
177 void internal() const { }
178 friend void operator+(int const &, Somewhat<T> const &) {} // expected-error{{redefinition of 'operator+'}}
179};
180
181void operator+(int const &, Somewhat<char> const &x) { // expected-note {{previous definition is here}}
182 x.internal(); // expected-note{{in instantiation of template class 'pr14785::Somewhat<char>' requested here}}
183}
184}
185
186namespace D30375 {
187template <typename K> struct B {
188 template <typename A> bool insert(A &);
189};
190
191template <typename K>
192template <typename A> bool B<K>::insert(A &x) { return x < x; }
193
194template <typename K> class D {
195 B<K> t;
196
197public:
198 K x;
199 bool insert() { return t.insert(x); }
200 template <typename K1> friend bool operator<(const D<K1> &, const D<K1> &);
201};
202
203template <typename K> bool operator<(const D<K> &, const D<K> &);
204
205void func() {
206 D<D<int>> cache;
207 cache.insert();
208}
209}