re PR libstdc++/38720 (_Relative_pointer_impl invokes undefined behavior)
[gcc.git] / libstdc++-v3 / testsuite / ext / ext_pointer / 1_neg.cc
1 // Bob Walters 10-2008
2
3 // Test for Container using non-standard pointer types.
4
5 // Copyright (C) 2008
6 // Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 2, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING. If not, write to the Free
21 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // USA.
23
24 // As a special exception, you may use this file as part of a free software
25 // library without restriction. Specifically, if other files instantiate
26 // templates or use macros or inline functions from this file, or you compile
27 // this file and link it with other files to produce an executable, this
28 // file does not by itself cause the resulting executable to be covered by
29 // the GNU General Public License. This exception does not however
30 // invalidate any other reasons why the executable file might be covered by
31 // the GNU General Public License.
32
33 // { dg-do compile }
34
35 #include <algorithm>
36 #include <testsuite_hooks.h>
37 #include <ext/pointer.h>
38
39 using __gnu_cxx::_Pointer_adapter;
40 using __gnu_cxx::_Relative_pointer_impl;
41 using __gnu_cxx::__static_pointer_cast;
42 using __gnu_cxx::__const_pointer_cast;
43
44
45 struct A {
46 int i;
47 };
48 struct B : public A{
49 int j;
50 };
51 typedef _Pointer_adapter<_Relative_pointer_impl<B> > B_pointer;
52 typedef _Pointer_adapter<_Relative_pointer_impl<const B> > const_B_pointer;
53 typedef _Pointer_adapter<_Relative_pointer_impl<A> > A_pointer;
54 typedef _Pointer_adapter<_Relative_pointer_impl<const A> > const_A_pointer;
55
56
57 void test01(void) {
58 bool test __attribute__((unused)) = true;
59
60 A a;
61 B b;
62
63 A_pointer aptr( &a );
64
65 // Can't implicitly cast from A* to B*
66 B_pointer bptr1(aptr); // { dg-error "instantiated from here" 31 }
67 B_pointer bptr2(&a); // { dg-error "instantiated from here" 32 }
68
69 // but explicit cast/conversion is OK.
70 B_pointer bptr3(__static_pointer_cast<B_pointer>(aptr)); // ok
71 B_pointer bptr4(__static_pointer_cast<B_pointer>(&a)); // ok
72
73 // Can't implicitly cast from A* to B*
74 bptr1 = aptr; // { dg-error "instantiated from here" 39 }
75 bptr1 = &a; // { dg-error "instantiated from here" 40 }
76
77 // but explicit cast/conversion is OK.
78 bptr1 = __static_pointer_cast<B_pointer>(aptr); // ok
79 bptr1 = __static_pointer_cast<B_pointer>(&a); // ok
80
81 // Similarly, can't shed constness via implicit cast
82 const_A_pointer captr(&a);
83 A_pointer aptr2(captr); // { dg-error "instantiated from here" 48 }
84
85 // but explicit cast/conversion is OK.
86 A_pointer aptr3(__const_pointer_cast<A_pointer>(captr)); // ok
87
88 // Similarly, can't shed constness via implicit cast
89 aptr2 = captr; // { dg-error "instantiated from here" 54 }
90
91 // but explicit cast/conversion is OK.
92 aptr3 = __const_pointer_cast<A_pointer>(captr); // ok
93
94 // Combine explicit const cast with implicit downcast.
95 const_B_pointer cbptr(&b);
96 A_pointer aptr4(cbptr); // { dg-error "instantiated from here" 61 }
97 aptr4 = cbptr; // { dg-error "instantiated from here" 62 }
98
99 A_pointer aptr5(__const_pointer_cast<B_pointer>(cbptr)); // ok
100 aptr5 = __const_pointer_cast<B_pointer>(cbptr); // ok
101 }
102
103 // { dg-error "invalid conversion " "" { target *-*-* } 299 }
104 // { dg-error "initializing argument 1 of" "" { target *-*-* } 299 }
105 // { dg-error "invalid conversion " "" { target *-*-* } 305 }
106 // { dg-error "initializing argument 1 of" "" { target *-*-* } 305 }
107 // { dg-error "invalid conversion " "" { target *-*-* } 322 }
108 // { dg-error "initializing argument 1 of" "" { target *-*-* } 322 }
109 // { dg-error "invalid conversion " "" { target *-*-* } 330 }
110 // { dg-error "initializing argument 1 of" "" { target *-*-* } 330 }
111 // { dg-excess-errors "In constructor" }
112