1_neg.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Bob Walters 10-2008
  2. // Test for Container using non-standard pointer types.
  3. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License along
  15. // with this library; see the file COPYING3. If not see
  16. // <http://www.gnu.org/licenses/>.
  17. // { dg-do compile }
  18. #include <algorithm>
  19. #include <testsuite_hooks.h>
  20. #include <ext/pointer.h>
  21. using __gnu_cxx::_Pointer_adapter;
  22. using __gnu_cxx::_Relative_pointer_impl;
  23. using __gnu_cxx::__static_pointer_cast;
  24. using __gnu_cxx::__const_pointer_cast;
  25. struct A {
  26. int i;
  27. };
  28. struct B : public A{
  29. int j;
  30. };
  31. typedef _Pointer_adapter<_Relative_pointer_impl<B> > B_pointer;
  32. typedef _Pointer_adapter<_Relative_pointer_impl<const B> > const_B_pointer;
  33. typedef _Pointer_adapter<_Relative_pointer_impl<A> > A_pointer;
  34. typedef _Pointer_adapter<_Relative_pointer_impl<const A> > const_A_pointer;
  35. void test01(void) {
  36. A a;
  37. B b;
  38. A_pointer aptr( &a );
  39. // Can't implicitly cast from A* to B*
  40. B_pointer bptr1(aptr); // { dg-error "required from here" 31 }
  41. B_pointer bptr2(&a); // { dg-error "required from here" 32 }
  42. // but explicit cast/conversion is OK.
  43. B_pointer bptr3(__static_pointer_cast<B_pointer>(aptr)); // ok
  44. B_pointer bptr4(__static_pointer_cast<B_pointer>(&a)); // ok
  45. // Can't implicitly cast from A* to B*
  46. bptr1 = aptr; // { dg-error "required from here" 39 }
  47. bptr1 = &a; // { dg-error "required from here" 40 }
  48. // but explicit cast/conversion is OK.
  49. bptr1 = __static_pointer_cast<B_pointer>(aptr); // ok
  50. bptr1 = __static_pointer_cast<B_pointer>(&a); // ok
  51. // Similarly, can't shed constness via implicit cast
  52. const_A_pointer captr(&a);
  53. A_pointer aptr2(captr); // { dg-error "required from here" 48 }
  54. // but explicit cast/conversion is OK.
  55. A_pointer aptr3(__const_pointer_cast<A_pointer>(captr)); // ok
  56. // Similarly, can't shed constness via implicit cast
  57. aptr2 = captr; // { dg-error "required from here" 54 }
  58. // but explicit cast/conversion is OK.
  59. aptr3 = __const_pointer_cast<A_pointer>(captr); // ok
  60. // Combine explicit const cast with implicit downcast.
  61. const_B_pointer cbptr(&b);
  62. A_pointer aptr4(cbptr); // { dg-error "required from here" 61 }
  63. aptr4 = cbptr; // { dg-error "required from here" 62 }
  64. A_pointer aptr5(__const_pointer_cast<B_pointer>(cbptr)); // ok
  65. aptr5 = __const_pointer_cast<B_pointer>(cbptr); // ok
  66. }
  67. // { dg-prune-output "include" }