1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>pointer_to_other
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1">
7 <body text=
"#000000" bgcolor=
"#ffffff" link=
"#0000ff" vlink=
"#0000ff">
8 <h1><img height=
"86" alt=
"boost.png (6897 bytes)" src=
"../../boost.png"
9 width=
"277" align=
"middle" border=
"0">pointer_to_other
</h1>
11 The pointer to other utility provides a way, given a source pointer type,
12 to obtain a pointer of the same type to another pointee type. The utility is
13 defined in
<cite><a href=
"../../boost/pointer_to_other.hpp">boost/pointer_to_other.hpp
</a>.
</cite></p>
14 <p>There is test/example code in
<cite><a href=
"test/pointer_to_other_test.cpp">pointer_to_other_test.cpp
</a></cite>.
</p>
15 <h2><a name=
"contents">Contents
</a></h2>
18 <a href=
"#rationale">Rationale
</a>
20 <a href=
"#synopsis">Synopsis
</a>
22 <a href=
"#example">Example
</a></li>
24 <h2><a name=
"rationale">Rationale
</a></h2>
25 <p>When building pointer independent classes, like memory managers, allocators, or
26 containers, there is often a need to define pointers generically, so that if a
27 template parameter represents a pointer (for example, a raw or smart pointer to
28 an int), we can define another pointer of the same type to another pointee (a
29 raw or smart pointer to a float.)
</p>
30 <pre>template
<class IntPtr
>
31 class FloatPointerHolder
33 <em>// Let's define a pointer to a float
</em>
34 typedef typename boost::pointer_to_other
35 <IntPtr, float
>::type float_ptr_t;
36 float_ptr_t float_ptr;
38 <h2><a name=
"synopsis">Synopsis
</a></h2>
42 template
<class T, class U
>
43 struct pointer_to_other;
45 template
<class T, class U, template
<class
> class Sp
>
46 struct pointer_to_other
< Sp
<T
>, U
>
48 typedef Sp
<U
> type;
51 template
<class T, class T2, class U,
52 template
<class, class
> class Sp
>
53 struct pointer_to_other
< Sp
<T, T2
>, U
>
55 typedef Sp
<U, T2
> type;
58 template
<class T, class T2, class T3, class U,
59 template
<class, class, class
> class Sp
>
60 struct pointer_to_other
< Sp
<T, T2, T3
>, U
>
62 typedef Sp
<U, T2, T3
> type;
65 template
<class T, class U
>
66 struct pointer_to_other
< T*, U
>
71 }
<em>// namespace boost
</em></pre>
72 <p>If these definitions are not correct for a specific smart pointer, we can define
73 a specialization of pointer_to_other.
</p>
74 <h2><a name=
"example">Example
</a></h2>
75 <pre><em>// Let's define a memory allocator that can
76 // work with raw and smart pointers
</em>
78 #include
<boost/pointer_to_other.hpp
>
80 template
<class VoidPtr
>
81 class memory_allocator
83 // Predefine a memory_block
</em>
86 // Define a pointer to a memory_block from a void pointer
87 // If VoidPtr is void *, block_ptr_t is block*
88 // If VoidPtr is smart_ptr
<void
>, block_ptr_t is smart_ptr
<block
></em>
89 typedef typename boost::pointer_to_other
90 <VoidPtr, block
>::type block_ptr_t;
95 block_ptr_t next_block;
98 block_ptr_t free_blocks;
100 <p>As we can see, using pointer_to_other we can create pointer independent code.
</p>
103 <p><small>Copyright
2005,
2006 Ion GaztaƱaga and Peter Dimov. Use, modification,
104 and distribution are subject to the Boost Software License, Version
1.0.
<br>
105 (See accompanying file
<a href=
"../../LICENSE_1_0.txt">LICENSE_1_0.txt
</a> or a
106 copy at
< <a href=
"http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt
</a>>.)
</small></p>