• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Polymorphic casts</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../conversion.html" title="Chapter 11. The Conversion Library 1.7">
10<link rel="prev" href="../conversion.html" title="Chapter 11. The Conversion Library 1.7">
11<link rel="next" href="synopsis.html" title="Synopsis">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="../conversion.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../conversion.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synopsis.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="conversion.polymorphic_casts"></a><a class="link" href="polymorphic_casts.html" title="Polymorphic casts">Polymorphic casts</a>
29</h2></div></div></div>
30<div class="toc"><dl class="toc">
31<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast">polymorphic_downcast</a></span></dt>
32<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_cast">polymorphic_cast</a></span></dt>
33<dt><span class="section"><a href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast">polymorphic_pointer_cast</a></span></dt>
34</dl></div>
35<p>
36      Pointers to polymorphic objects (objects of classes which define at least one
37      virtual function) are sometimes downcast or crosscast. Downcasting means casting
38      from a base class to a derived class. Crosscasting means casting across an
39      inheritance hierarchy diagram, such as from one base to the other in a <code class="literal">Y</code>
40      diagram hierarchy.
41    </p>
42<p>
43      Such casts can be done with old-style casts, but this approach is never to
44      be recommended. Old-style casts are sorely lacking in type safety, suffer poor
45      readability, and are difficult to locate with search tools.
46    </p>
47<div class="section">
48<div class="titlepage"><div><div><h3 class="title">
49<a name="conversion.polymorphic_casts.polymorphic_downcast"></a><a name="polymorphic_downcast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast" title="polymorphic_downcast">polymorphic_downcast</a>
50</h3></div></div></div>
51<p>
52        The C++ built-in <code class="computeroutput"><span class="keyword">static_cast</span></code>
53        can be used for efficiently downcasting pointers to polymorphic objects,
54        but provides no error detection for the case where the pointer being cast
55        actually points to the wrong derived class. The <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
56        template retains the efficiency of <code class="computeroutput"><span class="keyword">static_cast</span></code>
57        for non-debug compilations, but for debug compilations adds safety via an
58        <code class="computeroutput"><span class="identifier">assert</span><span class="special">()</span></code>
59        that a <code class="computeroutput"><span class="keyword">dynamic_cast</span></code> succeeds.
60      </p>
61<p>
62        A <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code> should
63        be used for downcasts that you are certain should succeed. Error checking
64        is only performed in translation units where <code class="computeroutput"><span class="identifier">NDEBUG</span></code>
65        is not defined, via
66      </p>
67<pre class="programlisting"><span class="identifier">assert</span><span class="special">(</span> <span class="keyword">dynamic_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">x</span> <span class="special">)</span>
68</pre>
69<p>
70        where <code class="computeroutput"><span class="identifier">x</span></code> is the source pointer.
71        This approach ensures that not only is a non-zero pointer returned, but also
72        that it is correct in the presence of multiple inheritance. Attempts to crosscast
73        using <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
74        will fail to compile.
75      </p>
76<div class="warning"><table border="0" summary="Warning">
77<tr>
78<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
79<th align="left">Warning</th>
80</tr>
81<tr><td align="left" valign="top"><p>
82          Because <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
83          uses <code class="computeroutput"><span class="identifier">assert</span><span class="special">()</span></code>,
84          it violates the One Definition Rule (ODR) if <code class="computeroutput"><span class="identifier">NDEBUG</span></code>
85          is inconsistently defined across translation units. See ISO Std 3.2
86        </p></td></tr>
87</table></div>
88<h5>
89<a name="conversion.polymorphic_casts.polymorphic_downcast.h0"></a>
90        <span class="phrase"><a name="conversion.polymorphic_casts.polymorphic_downcast.example_"></a></span><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_downcast.example_">Example:</a>
91      </h5>
92<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">polymorphic_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
93<span class="special">...</span>
94<span class="keyword">class</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Fruit</span><span class="special">(){};</span> <span class="special">...</span> <span class="special">};</span>
95<span class="keyword">class</span> <span class="identifier">Banana</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="special">...</span> <span class="special">};</span>
96<span class="special">...</span>
97<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span> <span class="identifier">Fruit</span> <span class="special">*</span> <span class="identifier">fruit</span> <span class="special">)</span> <span class="special">{</span>
98  <span class="comment">// ... logic which leads us to believe it is a Banana</span>
99  <span class="identifier">Banana</span> <span class="special">*</span> <span class="identifier">banana</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">polymorphic_downcast</span><span class="special">&lt;</span><span class="identifier">Banana</span><span class="special">*&gt;(</span><span class="identifier">fruit</span><span class="special">);</span>
100  <span class="special">...</span>
101<span class="special">}</span>
102</pre>
103</div>
104<div class="section">
105<div class="titlepage"><div><div><h3 class="title">
106<a name="conversion.polymorphic_casts.polymorphic_cast"></a><a name="polymorphic_cast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_cast" title="polymorphic_cast">polymorphic_cast</a>
107</h3></div></div></div>
108<p>
109        The C++ built-in <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
110        can be used for downcasts and crosscasts of pointers to polymorphic objects,
111        but error notification in the form of a returned value of 0 is inconvenient
112        to test, or worse yet, easy to forget to test. The throwing form of <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>, which works on references, can
113        be used on pointers through the ugly expression <code class="computeroutput"><span class="special">&amp;</span><span class="keyword">dynamic_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&amp;&gt;(*</span><span class="identifier">p</span><span class="special">)</span></code>, which
114        causes undefined behavior if <code class="computeroutput"><span class="identifier">p</span></code>
115        is <code class="computeroutput"><span class="number">0</span></code>. The <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code>
116        template performs a <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
117        on a pointer, and throws an exception if the <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
118        returns 0.
119      </p>
120<p>
121        For crosscasts, or when the success of a cast can only be known at runtime,
122        or when efficiency is not important, <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code>
123        is preferred.
124      </p>
125<p>
126        The C++ built-in <code class="computeroutput"><span class="keyword">dynamic_cast</span></code>
127        must be used to cast references rather than pointers. It is also the only
128        cast that can be used to check whether a given interface is supported; in
129        that case a return of 0 isn't an error condition.
130      </p>
131</div>
132<div class="section">
133<div class="titlepage"><div><div><h3 class="title">
134<a name="conversion.polymorphic_casts.polymorphic_pointer_cast"></a><a name="polymorphic_pointer_cast"></a><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast" title="polymorphic_pointer_cast">polymorphic_pointer_cast</a>
135</h3></div></div></div>
136<p>
137        While <code class="computeroutput"><span class="identifier">polymorphic_downcast</span></code>
138        and <code class="computeroutput"><span class="identifier">polymorphic_cast</span></code> work
139        with built-in pointer types only, <code class="computeroutput"><span class="identifier">polymorphic_pointer_downcast</span></code>
140        and <code class="computeroutput"><span class="identifier">polymorphic_pointer_cast</span></code>
141        are more generic versions with support for any pointer type for which the
142        following expressions would be valid:
143      </p>
144<p>
145        For <code class="computeroutput"><span class="identifier">polymorphic_pointer_downcast</span></code>:
146      </p>
147<pre class="programlisting"><span class="identifier">static_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
148<span class="identifier">dynamic_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
149</pre>
150<p>
151        For <code class="computeroutput"><span class="identifier">polymorphic_pointer_cast</span></code>:
152      </p>
153<pre class="programlisting"><span class="identifier">dynamic_pointer_cast</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">);</span>
154<span class="special">!</span><span class="identifier">p</span><span class="special">;</span> <span class="comment">// conversion to bool with negation</span>
155</pre>
156<p>
157        This includes C++ built-in pointers, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span></code>,
158        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span></code>, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive_ptr</span></code>,
159        etc.
160      </p>
161<h5>
162<a name="conversion.polymorphic_casts.polymorphic_pointer_cast.h0"></a>
163        <span class="phrase"><a name="conversion.polymorphic_casts.polymorphic_pointer_cast.example_"></a></span><a class="link" href="polymorphic_casts.html#conversion.polymorphic_casts.polymorphic_pointer_cast.example_">Example:</a>
164      </h5>
165<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">polymorphic_pointer_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
166
167<span class="keyword">class</span> <span class="identifier">Fruit</span> <span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Fruit</span><span class="special">(){}</span> <span class="special">};</span>
168<span class="keyword">class</span> <span class="identifier">Banana</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Fruit</span> <span class="special">{};</span>
169
170<span class="comment">// Use one of these:</span>
171<span class="keyword">typedef</span> <span class="identifier">Fruit</span><span class="special">*</span> <span class="identifier">FruitPtr</span><span class="special">;</span>
172<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;</span> <span class="identifier">FruitPtr</span><span class="special">;</span>
173<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;</span> <span class="identifier">FruitPtr</span><span class="special">;</span>
174<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">intrusive_ptr</span><span class="special">&lt;</span><span class="identifier">Fruit</span><span class="special">&gt;</span> <span class="identifier">FruitPtr</span><span class="special">;</span>
175
176<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">FruitPtr</span> <span class="identifier">fruit</span><span class="special">)</span> <span class="special">{</span>
177  <span class="comment">// ... logic which leads us to believe it is a banana</span>
178  <span class="keyword">auto</span> <span class="identifier">banana</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">polymorphic_pointer_downcast</span><span class="special">&lt;</span><span class="identifier">Banana</span><span class="special">&gt;(</span><span class="identifier">fruit</span><span class="special">);</span>
179  <span class="special">...</span>
180<span class="special">}</span>
181</pre>
182</div>
183</div>
184<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
185<td align="left"></td>
186<td align="right"><div class="copyright-footer">Copyright © 2001 Beman Dawes<br>Copyright © 2014-2020 Antony Polukhin<p>
187        Distributed under the Boost Software License, Version 1.0. (See accompanying
188        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
189      </p>
190</div></td>
191</tr></table>
192<hr>
193<div class="spirit-nav">
194<a accesskey="p" href="../conversion.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../conversion.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="synopsis.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
195</div>
196</body>
197</html>
198