conversion-function.cpp revision 79b680ea869983d62b84a9175eeb6b6efb376326
1// RUN: clang-cc -fsyntax-only -verify %s
2class X {
3public:
4  operator bool();
5  operator int() const;
6
7  bool f() {
8    return operator bool();
9  }
10
11  float g() {
12    return operator float(); // expected-error{{no matching function for call to 'operator float'}}
13  }
14};
15
16operator int(); // expected-error{{conversion function must be a non-static member function}}
17
18operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
19
20typedef int func_type(int);
21typedef int array_type[10];
22
23class Y {
24public:
25  void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \
26  // expected-error{{conversion function cannot have any parameters}}
27
28  operator float(...) const;  // expected-error{{conversion function cannot be variadic}}
29
30
31  operator func_type(); // expected-error{{conversion function cannot convert to a function type}}
32  operator array_type(); // expected-error{{conversion function cannot convert to an array type}}
33};
34
35
36typedef int INT;
37typedef INT* INT_PTR;
38
39class Z {
40  operator int(); // expected-note {{previous declaration is here}}
41  operator int**(); // expected-note {{previous declaration is here}}
42
43  operator INT();  // expected-error{{conversion function cannot be redeclared}}
44  operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}}
45};
46
47
48class A { };
49
50class B : public A {
51public:
52  operator A&() const; // expected-warning{{conversion function converting 'class B' to its base class 'class A' will never be used}}
53  operator const void() const; // expected-warning{{conversion function converting 'class B' to 'void const' will never be used}}
54  operator const B(); // expected-warning{{conversion function converting 'class B' to itself will never be used}}
55};
56
57// This used to crash Clang.
58struct Flip;
59struct Flop {
60  Flop();
61  Flop(const Flip&);
62};
63struct Flip {
64  operator Flop() const;
65};
66Flop flop = Flip(); // expected-error {{cannot initialize 'flop' with an rvalue of type 'struct Flip'}}
67
68// This tests that we don't add the second conversion declaration to the list of user conversions
69struct C {
70  operator const char *() const;
71};
72
73C::operator const char*() const { return 0; }
74
75void f(const C& c) {
76  const char* v = c;
77}
78
79// Test. Conversion in base class is visible in derived class.
80class XB {
81public:
82  operator int(); // expected-note {{candidate function}}
83};
84
85class Yb : public XB {
86public:
87  operator char(); // expected-note {{candidate function}}
88};
89
90void f(Yb& a) {
91  if (a) { } // expected-error {{conversion from 'class Yb' to 'bool' is ambiguous}}
92  int i = a; // OK. calls XB::operator int();
93  char ch = a;  // OK. calls Yb::operator char();
94}
95
96// Test conversion + copy construction.
97class AutoPtrRef { };
98
99class AutoPtr {
100  // FIXME: Using 'unavailable' since we do not have access control yet.
101  // FIXME: The error message isn't so good.
102  AutoPtr(AutoPtr &) __attribute__((unavailable));
103
104public:
105  AutoPtr();
106  AutoPtr(AutoPtrRef);
107
108  operator AutoPtrRef();
109};
110
111AutoPtr make_auto_ptr();
112
113AutoPtr test_auto_ptr(bool Cond) {
114  AutoPtr p1( make_auto_ptr() );
115
116  AutoPtr p;
117  if (Cond)
118    return p; // expected-error{{incompatible type returning}}
119
120  return AutoPtr();
121}
122
123
124