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