1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
3 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify=expected,cxx98_11,cxx11 -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
4 // RUN: %clang_cc1 -std=c++98 -fsyntax-only -verify=expected,cxx98_11,cxx98 -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
12 return operator bool();
16 return operator float(); // expected-error{{use of undeclared 'operator float'}}
19 static operator short(); // expected-error{{conversion function must be a non-static member function}}
22 operator int(); // expected-error{{conversion function must be a non-static member function}}
24 operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
26 typedef int func_type(int);
27 typedef int array_type
[10];
31 void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \
32 // expected-error{{conversion function cannot have any parameters}}
34 operator bool(int a
= 4, int b
= 6) const; // expected-error{{conversion function cannot have any parameters}}
37 operator float(...) const; // expected-error{{conversion function cannot be variadic}}
40 operator func_type(); // expected-error{{conversion function cannot convert to a function type}}
41 operator array_type(); // expected-error{{conversion function cannot convert to an array type}}
49 operator int(); // expected-note {{previous declaration is here}}
50 operator int**(); // expected-note {{previous declaration is here}}
52 operator INT(); // expected-error{{conversion function cannot be redeclared}}
53 operator INT_PTR
*(); // expected-error{{conversion function cannot be redeclared}}
61 operator A
&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}}
62 operator const void() const; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}}
63 operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}}
70 virtual operator BaseA
&() = 0;
71 virtual operator DerivedA
&() = 0;
74 class DerivedA
: public BaseA
, BaseB
{
75 virtual operator BaseA
&(); // OK. Overrides BaseB::operatorBaseA&()
76 virtual operator DerivedA
&(); // OK. Overrides BaseB::operatorDerivedA&()
79 class DerivedB
: public BaseA
{
80 virtual operator DerivedB
&(); // expected-warning{{conversion function converting 'DerivedB' to itself will never be used}}
81 virtual operator BaseA
&(); // expected-warning{{conversion function converting 'DerivedB' to its base class 'BaseA' will never be used}}
84 // This used to crash Clang.
88 Flop(const Flip
&); // expected-note{{candidate constructor}}
91 operator Flop() const; // expected-note{{candidate function}}
93 Flop flop
= Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}}
95 // This tests that we don't add the second conversion declaration to the list of user conversions
97 operator const char *() const;
100 C::operator const char*() const { return 0; }
106 // Test. Conversion in base class is visible in derived class.
109 operator int(); // expected-note {{candidate function}}
112 class Yb
: public XB
{
114 operator char(); // expected-note {{candidate function}}
118 if (a
) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}}
119 int i
= a
; // OK. calls XB::operator int();
120 char ch
= a
; // OK. calls Yb::operator char();
123 // Test conversion + copy construction. This is a pure C++98 test.
124 // However we may extend implicit moves into C++98, we must make sure the
125 // result here is not changed.
126 class AutoPtrRef
{ };
129 AutoPtr(AutoPtr
&); // cxx98-note {{declared private here}}
135 operator AutoPtrRef();
138 AutoPtr
make_auto_ptr();
140 AutoPtr
test_auto_ptr(bool Cond
) {
141 AutoPtr
p1( make_auto_ptr() );
145 return p
; // cxx98-error {{calling a private constructor}}
155 A1(const A1
&); // cxx98_11-note 2 {{declared private here}}
159 // FIXME: redundant diagnostics!
160 return "Hello"; // cxx98_11-error {{calling a private constructor}}
161 // cxx98-warning@-1 {{an accessible copy constructor}}
162 // cxx11-warning@-2 {{copying parameter of type 'A1' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
165 namespace source_locations
{
171 template<typename T
, typename U
>
175 struct A
<T
, T
> : A
<T
, int> { };
179 operator A
<T
, typename sneaky_int
<T
>::type
>&() const; // expected-note{{candidate function}}
183 A
<float, float> &af
= E(); // expected-error{{no viable conversion}}
184 A
<float, int> &af2
= E();
185 const A
<float, int> &caf2
= E();
192 * // expected-error{{'operator type-parameter-0-0 *' declared as a pointer to a reference of type 'int &'}}
196 E2
<int&> e2i
; // expected-note{{in instantiation}}
199 namespace crazy_declarators
{
201 (&operator bool())(); // expected-error {{use a typedef to declare a conversion to 'bool (&)()'}}
202 *operator int(); // expected-error {{put the complete type after 'operator'}}
203 // No suggestion of using a typedef here; that's not possible.
204 template<typename T
> (&operator T())();
205 #if __cplusplus <= 199711L
206 // expected-error-re@-2 {{cannot specify any part of a return type in the declaration of a conversion function{{$}}}}
208 // 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 (&)()'{{$}}}}
214 namespace smart_ptr
{
224 operator YRef(); // expected-note{{candidate function}}
227 struct X
{ // expected-note{{candidate constructor (the implicit copy constructor) not}}
228 #if __cplusplus >= 201103L
229 // expected-note@-2 {{candidate constructor (the implicit move constructor) not}}
232 explicit X(Y
); // expected-note {{not a candidate}}
238 X x
= make_Y(); // expected-error{{no viable conversion from 'Y' to 'X'}}
249 Other(const Other
&);
255 #if __cplusplus <= 199711L
256 // expected-error@-2 {{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}}
258 // expected-error@-4 {{cannot pass object of non-trivial type 'Other' through variadic constructor; call will abort at runtime}}
263 // Make sure that we don't allow too many conversions in an
264 // auto_ptr-like template. In particular, we can't create multiple
265 // temporary objects when binding to a reference.
267 struct auto_ptr_ref
{ };
270 auto_ptr(auto_ptr_ref
);
271 explicit auto_ptr(int *);
273 operator auto_ptr_ref();
281 X
x(auto_ptr(new int));
282 return X(auto_ptr(new int));
297 typedef unsigned char uint8
;
300 MutablePtr() : ptr(0) {}
303 operator void*() { return ptr
; }
306 operator uint8
*() { return reinterpret_cast<uint8
*>(ptr
); }
307 operator const char*() const { return reinterpret_cast<const char*>(ptr
); }
310 void fake_memcpy(const void *);
318 namespace rdar8018274
{
321 operator const struct X
*() const;
325 operator struct X
* ();
330 (void) (x
!= __null
);
338 struct Derived1
: Base
{ };
340 struct Derived2
: Base
{ };
342 struct SuperDerived
: Derived1
, Derived2
{
343 using Derived1::operator int;
346 struct UeberDerived
: SuperDerived
{
350 void test2(UeberDerived ud
) {
351 int i
= ud
; // expected-error{{ambiguous conversion from derived class 'UeberDerived' to base class 'rdar8018274::Base'}}
362 struct Derived23
: Base2
, Base3
{
363 using Base2::operator int;
366 struct ExtraDerived23
: Derived23
{ };
368 void test3(ExtraDerived23 ed
) {
374 template <typename T
> struct Iterator
;
375 template <typename T
> struct Container
;
378 struct Iterator
<int> {
379 typedef Container
<int> container_type
;
382 template <typename T
>
384 typedef typename Iterator
<T
>::container_type X
;
385 operator X(void) { return X(); }
396 template <typename T
> operator T();
398 int x
= C().operator int();
405 template<class Container
>
419 const T
& operator[](int)const;
422 generic_list
<generic_list
<int> > l
;
423 array
<array
<int> > a
= l
;
435 A
& a3
= static_cast<A
&>(c
);
448 A
f(const C c
) { return c
; }
453 operator enum E
{ e
} (); // expected-error {{'PR18234::A::E' cannot be defined in a type specifier}}
454 operator struct S
{ int n
; } (); // expected-error {{'PR18234::A::S' cannot be defined in a type specifier}}
455 // expected-note@-1 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'struct A' to 'const S &' for 1st argument}}
456 #if __cplusplus >= 201103L
457 // expected-note@-3 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'struct A' to 'S &&' for 1st argument}}
460 A::S s
= a
; // expected-error {{no viable conversion from 'struct A' to 'A::S'}}
462 bool k1
= e
== A::e
; // expected-error {{no member named 'e'}}
468 const operator int(); // expected-error {{cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator'}}
469 const operator int() const; // expected-error {{cannot specify any part of a return type}}
470 volatile const operator int(); // expected-error {{cannot specify any part of a return type}}
472 operator const int() const;
476 #if __cplusplus >= 201103L
477 namespace dependent_conversion_function_id_lookup
{
482 template<class T
> struct C
{
483 template <typename U
> using Lookup
= decltype(T
{}.operator U());
487 template<typename T
> struct A2
{
490 template<typename T
> struct B
: A2
<T
> {
491 template<typename U
> using Lookup
= decltype(&B::operator U
);
493 using Result
= B
<int>::Lookup
<int>;
494 using Result
= int (A2
<int>::*)();