blob: 32a1cfdfec9db9ad4cb9a00fa3356cfd29975ced [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Li64a1a812016-04-13 20:00:45 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
Douglas Gregor71b6a372009-05-19 19:05:47 +00004
Charles Li64a1a812016-04-13 20:00:45 +00005struct 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}}
Douglas Gregor71b6a372009-05-19 19:05:47 +000011
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) {
John McCall909acf82011-02-14 18:34:10 +000033 (void)((U)t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
Douglas Gregor71b6a372009-05-19 19:05:47 +000034 }
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) {
John McCall909acf82011-02-14 18:34:10 +000046 (void)static_cast<U>(t); // expected-error{{no matching conversion for static_cast from 'int' to 'A'}}
Douglas Gregor71b6a372009-05-19 19:05:47 +000047 }
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) {
Douglas Gregorb472e932011-04-15 17:59:54 +000073 (void)reinterpret_cast<U>(t); // expected-error{{qualifiers}}
Douglas Gregor71b6a372009-05-19 19:05:47 +000074 }
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) {
John McCall909acf82011-02-14 18:34:10 +000099 (void)U(t); // expected-error{{cannot convert 'A' to 'int' without a conversion operator}}
Douglas Gregor71b6a372009-05-19 19:05:47 +0000100 }
101};
102
103template struct FunctionalCast1<int, float>;
104template struct FunctionalCast1<A, int>; // expected-note{{instantiation}}
105
Douglas Gregor71b6a372009-05-19 19:05:47 +0000106// 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>;
Douglas Gregore6fb91f2009-10-29 23:08:22 +0000115
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}