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