• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4 
5 struct A { int x; };
6 // expected-note@-1 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const A' for 1st argument}}
7 #if __cplusplus >= 201103L
8 // expected-note@-3 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'A' for 1st argument}}
9 #endif
10 // expected-note@-5 {{candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided}}
11 
12 class Base {
13 public:
14   virtual void f();
15 };
16 
17 class Derived : public Base { };
18 
19 struct ConvertibleToInt {
20   operator int() const;
21 };
22 
23 struct Constructible {
24   Constructible(int, float);
25 };
26 
27 // ---------------------------------------------------------------------
28 // C-style casts
29 // ---------------------------------------------------------------------
30 template<typename T, typename U>
31 struct CStyleCast0 {
fCStyleCast032   void f(T t) {
33     (void)((U)t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
34   }
35 };
36 
37 template struct CStyleCast0<int, float>;
38 template struct CStyleCast0<A, int>; // expected-note{{instantiation}}
39 
40 // ---------------------------------------------------------------------
41 // static_cast
42 // ---------------------------------------------------------------------
43 template<typename T, typename U>
44 struct StaticCast0 {
fStaticCast045   void f(T t) {
46     (void)static_cast<U>(t); // expected-error{{no matching conversion for static_cast from 'int' to 'A'}}
47   }
48 };
49 
50 template struct StaticCast0<ConvertibleToInt, bool>;
51 template struct StaticCast0<int, float>;
52 template struct StaticCast0<int, A>; // expected-note{{instantiation}}
53 
54 // ---------------------------------------------------------------------
55 // dynamic_cast
56 // ---------------------------------------------------------------------
57 template<typename T, typename U>
58 struct DynamicCast0 {
fDynamicCast059   void f(T t) {
60     (void)dynamic_cast<U>(t); // expected-error{{not a reference or pointer}}
61   }
62 };
63 
64 template struct DynamicCast0<Base*, Derived*>;
65 template struct DynamicCast0<Base*, A>; // expected-note{{instantiation}}
66 
67 // ---------------------------------------------------------------------
68 // reinterpret_cast
69 // ---------------------------------------------------------------------
70 template<typename T, typename U>
71 struct ReinterpretCast0 {
fReinterpretCast072   void f(T t) {
73     (void)reinterpret_cast<U>(t); // expected-error{{qualifiers}}
74   }
75 };
76 
77 template struct ReinterpretCast0<void (*)(int), void (*)(float)>;
78 template struct ReinterpretCast0<int const *, float *>; // expected-note{{instantiation}}
79 
80 // ---------------------------------------------------------------------
81 // const_cast
82 // ---------------------------------------------------------------------
83 template<typename T, typename U>
84 struct ConstCast0 {
fConstCast085   void f(T t) {
86     (void)const_cast<U>(t); // expected-error{{not allowed}}
87   }
88 };
89 
90 template struct ConstCast0<int const * *, int * *>;
91 template struct ConstCast0<int const *, float *>; // expected-note{{instantiation}}
92 
93 // ---------------------------------------------------------------------
94 // C++ functional cast
95 // ---------------------------------------------------------------------
96 template<typename T, typename U>
97 struct FunctionalCast1 {
fFunctionalCast198   void f(T t) {
99     (void)U(t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
100   }
101 };
102 
103 template struct FunctionalCast1<int, float>;
104 template struct FunctionalCast1<A, int>; // expected-note{{instantiation}}
105 
106 // Generates temporaries, which we cannot handle yet.
107 template<int N, long M>
108 struct FunctionalCast2 {
fFunctionalCast2109   void f() {
110     (void)Constructible(N, M);
111   }
112 };
113 
114 template struct FunctionalCast2<1, 3>;
115 
116 // ---------------------------------------------------------------------
117 // implicit casting
118 // ---------------------------------------------------------------------
119 template<typename T>
120 struct Derived2 : public Base { };
121 
test_derived_to_base(Base * & bp,Derived2<int> * dp)122 void test_derived_to_base(Base *&bp, Derived2<int> *dp) {
123   bp = dp;
124 }
125