Clang Project

clang_source_code/test/SemaTemplate/instantiate-cast.cpp
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
5struct 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
12class Base { 
13public:
14  virtual void f();
15};
16
17class Derived : public Base { };
18
19struct ConvertibleToInt {
20  operator int() const;
21};
22
23struct Constructible {
24  Constructible(int, float);
25};
26
27// ---------------------------------------------------------------------
28// C-style casts
29// ---------------------------------------------------------------------
30template<typename T, typename U>
31struct CStyleCast0 {
32  void f(T t) {
33    (void)((U)t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
34  }
35};
36
37template struct CStyleCast0<int, float>;
38template struct CStyleCast0<A, int>; // expected-note{{instantiation}}
39
40// ---------------------------------------------------------------------
41// static_cast
42// ---------------------------------------------------------------------
43template<typename T, typename U>
44struct StaticCast0 {
45  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
50template struct StaticCast0<ConvertibleToInt, bool>;
51template struct StaticCast0<int, float>;
52template struct StaticCast0<int, A>; // expected-note{{instantiation}}
53
54// ---------------------------------------------------------------------
55// dynamic_cast
56// ---------------------------------------------------------------------
57template<typename T, typename U>
58struct DynamicCast0 {
59  void f(T t) {
60    (void)dynamic_cast<U>(t); // expected-error{{not a reference or pointer}}
61  }
62};
63
64template struct DynamicCast0<Base*, Derived*>;
65template struct DynamicCast0<Base*, A>; // expected-note{{instantiation}}
66
67// ---------------------------------------------------------------------
68// reinterpret_cast
69// ---------------------------------------------------------------------
70template<typename T, typename U>
71struct ReinterpretCast0 {
72  void f(T t) {
73    (void)reinterpret_cast<U>(t); // expected-error{{qualifiers}}
74  }
75};
76
77template struct ReinterpretCast0<void (*)(int), void (*)(float)>;
78template struct ReinterpretCast0<int const *, float *>; // expected-note{{instantiation}}
79
80// ---------------------------------------------------------------------
81// const_cast
82// ---------------------------------------------------------------------
83template<typename T, typename U>
84struct ConstCast0 {
85  void f(T t) {
86    (void)const_cast<U>(t); // expected-error{{not allowed}}
87  }
88};
89
90template struct ConstCast0<int const * *, int * *>;
91template struct ConstCast0<int const *, float *>; // expected-note{{instantiation}}
92
93// ---------------------------------------------------------------------
94// C++ functional cast
95// ---------------------------------------------------------------------
96template<typename T, typename U>
97struct FunctionalCast1 {
98  void f(T t) {
99    (void)U(t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
100  }
101};
102
103template struct FunctionalCast1<int, float>;
104template struct FunctionalCast1<A, int>; // expected-note{{instantiation}}
105
106// Generates temporaries, which we cannot handle yet.
107template<int N, long M>
108struct FunctionalCast2 {
109  void f() {
110    (void)Constructible(N, M);
111  }
112};
113
114template struct FunctionalCast2<1, 3>;
115
116// ---------------------------------------------------------------------
117// implicit casting
118// ---------------------------------------------------------------------
119template<typename T>
120struct Derived2 : public Base { };
121
122void test_derived_to_base(Base *&bp, Derived2<int> *dp) {
123  bp = dp;
124}
125