1 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -Wbind-to-temporary-copy -verify %s
2 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -Wbind-to-temporary-copy -verify -std=c++98 %s
3 // RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -Wbind-to-temporary-copy -verify -std=c++11 %s
11 return operator bool();
15 return operator float(); // expected-error{{use of undeclared 'operator float'}}
18 static operator short(); // expected-error{{conversion function must be a non-static member function}}
21 operator int(); // expected-error{{conversion function must be a non-static member function}}
23 operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
25 typedef int func_type(int);
26 typedef int array_type[10];
30 void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \
31 // expected-error{{conversion function cannot have any parameters}}
33 operator bool(int a = 4, int b = 6) const; // expected-error{{conversion function cannot have any parameters}}
36 operator float(...) const; // expected-error{{conversion function cannot be variadic}}
39 operator func_type(); // expected-error{{conversion function cannot convert to a function type}}
40 operator array_type(); // expected-error{{conversion function cannot convert to an array type}}
48 operator int(); // expected-note {{previous declaration is here}}
49 operator int**(); // expected-note {{previous declaration is here}}
51 operator INT(); // expected-error{{conversion function cannot be redeclared}}
52 operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}}
60 operator A&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}}
61 operator const void() const; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}}
62 operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}}
65 // This used to crash Clang.
69 Flop(const Flip&); // expected-note{{candidate constructor}}
72 operator Flop() const; // expected-note{{candidate function}}
74 Flop flop = Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}}
76 // This tests that we don't add the second conversion declaration to the list of user conversions
78 operator const char *() const;
81 C::operator const char*() const { return 0; }
87 // Test. Conversion in base class is visible in derived class.
90 operator int(); // expected-note {{candidate function}}
93 class Yb : public XB {
95 operator char(); // expected-note {{candidate function}}
99 if (a) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}}
100 int i = a; // OK. calls XB::operator int();
101 char ch = a; // OK. calls Yb::operator char();
104 // Test conversion + copy construction.
105 class AutoPtrRef { };
108 AutoPtr(AutoPtr &); // expected-note{{declared private here}}
114 operator AutoPtrRef();
117 AutoPtr make_auto_ptr();
119 AutoPtr test_auto_ptr(bool Cond) {
120 AutoPtr p1( make_auto_ptr() );
124 return p; // expected-error{{calling a private constructor}}
134 A1(const A1&); // expected-note 2 {{declared private here}}
138 // FIXME: redundant diagnostics!
139 return "Hello"; // expected-error {{calling a private constructor}}
140 #if __cplusplus <= 199711L
141 // expected-warning@-2 {{an accessible copy constructor}}
143 // expected-warning@-4 {{copying parameter of type 'A1' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
147 namespace source_locations {
153 template<typename T, typename U>
157 struct A<T, T> : A<T, int> { };
161 operator A<T, typename sneaky_int<T>::type>&() const; // expected-note{{candidate function}}
165 A<float, float> &af = E(); // expected-error{{no viable conversion}}
166 A<float, int> &af2 = E();
167 const A<float, int> &caf2 = E();
174 * // expected-error{{pointer to a reference}}
178 E2<int&> e2i; // expected-note{{in instantiation}}
181 namespace crazy_declarators {
183 (&operator bool())(); // expected-error {{use a typedef to declare a conversion to 'bool (&)()'}}
184 *operator int(); // expected-error {{put the complete type after 'operator'}}
185 // No suggestion of using a typedef here; that's not possible.
186 template<typename T> (&operator T())();
187 #if __cplusplus <= 199711L
188 // expected-error-re@-2 {{cannot specify any part of a return type in the declaration of a conversion function{{$}}}}
190 // expected-error-re@-4 {{cannot specify any part of a return type in the declaration of a conversion function; use an alias template to declare a conversion to 'T (&)()'{{$}}}}
196 namespace smart_ptr {
206 operator YRef(); // expected-note{{candidate function}}
209 struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}}
210 #if __cplusplus >= 201103L
211 // expected-note@-2 {{candidate constructor (the implicit move constructor) not}}
220 X x = make_Y(); // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}}
231 Other(const Other &);
237 #if __cplusplus <= 199711L
238 // expected-error@-2 {{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}}
240 // expected-error@-4 {{cannot pass object of non-trivial type 'Other' through variadic constructor; call will abort at runtime}}
245 // Make sure that we don't allow too many conversions in an
246 // auto_ptr-like template. In particular, we can't create multiple
247 // temporary objects when binding to a reference.
249 struct auto_ptr_ref { };
252 auto_ptr(auto_ptr_ref);
253 explicit auto_ptr(int *);
255 operator auto_ptr_ref();
263 X x(auto_ptr(new int));
264 return X(auto_ptr(new int));
279 typedef unsigned char uint8;
282 MutablePtr() : ptr(0) {}
285 operator void*() { return ptr; }
288 operator uint8*() { return reinterpret_cast<uint8*>(ptr); }
289 operator const char*() const { return reinterpret_cast<const char*>(ptr); }
292 void fake_memcpy(const void *);
300 namespace rdar8018274 {
303 operator const struct X *() const;
307 operator struct X * ();
312 (void) (x != __null);
320 struct Derived1 : Base { };
322 struct Derived2 : Base { };
324 struct SuperDerived : Derived1, Derived2 {
325 using Derived1::operator int;
328 struct UeberDerived : SuperDerived {
332 void test2(UeberDerived ud) {
333 int i = ud; // expected-error{{ambiguous conversion from derived class 'rdar8018274::SuperDerived' to base class 'rdar8018274::Base'}}
344 struct Derived23 : Base2, Base3 {
345 using Base2::operator int;
348 struct ExtraDerived23 : Derived23 { };
350 void test3(ExtraDerived23 ed) {
356 template <typename T> struct Iterator;
357 template <typename T> struct Container;
360 struct Iterator<int> {
361 typedef Container<int> container_type;
364 template <typename T>
366 typedef typename Iterator<T>::container_type X;
367 operator X(void) { return X(); }
378 template <typename T> operator T();
380 int x = C().operator int();
387 template<class Container>
401 const T& operator[](int)const;
404 generic_list<generic_list<int> > l;
405 array<array<int> > a = l;
417 A& a3 = static_cast<A&>(c);
430 A f(const C c) { return c; }
435 operator enum E { e } (); // expected-error {{'PR18234::A::E' cannot be defined in a type specifier}}
436 operator struct S { int n; } (); // expected-error {{'PR18234::A::S' cannot be defined in a type specifier}}
437 // expected-note@-1 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'struct A' to 'const PR18234::A::S &' for 1st argument}}
438 #if __cplusplus >= 201103L
439 // expected-note@-3 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'struct A' to 'PR18234::A::S &&' for 1st argument}}
442 A::S s = a; // expected-error {{no viable conversion from 'struct A' to 'A::S'}}
443 A::E e = a; // expected-note {{here}}
444 bool k1 = e == A::e; // expected-error {{no member named 'e'}}