instantiate-cast.cpp 3.5 KB

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