smart_cast.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!--
  4. (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. -->
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <link rel="stylesheet" type="text/css" href="../../../boost.css">
  12. <link rel="stylesheet" type="text/css" href="style.css">
  13. <title>Serialization - BOOST_STATIC_WARNING</title>
  14. </head>
  15. <body link="#0000ff" vlink="#800080">
  16. <table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header">
  17. <tr>
  18. <td valign="top" width="300">
  19. <h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../boost.png" border="0"></a></h3>
  20. </td>
  21. <td valign="top">
  22. <h1 align="center">Serialization</h1>
  23. <h2 align="center"><code>smart_cast</code></h2>
  24. </td>
  25. </tr>
  26. </table>
  27. <hr>
  28. <h3>Motivation</h3>
  29. To cast from one type to another related type, C++ provides the following
  30. operators:
  31. <dl>
  32. <dt><code>static_cast&lt;T *<&gt;(U *)<br>static_cast&lt;T &<&gt;(U &)</code></dt>
  33. <dd>
  34. <ul>
  35. <li>required if neither T nor U are polymorphic
  36. <li>permitted in other cases.
  37. <li>fails to detect erroneous casts of polymophic pointers/references at runtime.
  38. <li>does not permit "cross casting"
  39. <li>inline function calls can be optimized away during compile time.
  40. </ul>
  41. </dd>
  42. <p>
  43. <dt><code>dynamic_cast&lt;T *<&gt;(U *)<br>dynamic_cast&lt;T &<&gt;(U &)</code></dt>
  44. <dd>
  45. <ul>
  46. <li>permitted if either T or U are polymorphic
  47. <li>prohibited in other cases.
  48. <li>throws exception on detecting erroneous casts of polymorphic pointers/references
  49. at runtime.
  50. <li>permits "cross casting"
  51. <li>cannot optimise inline virtual functions at compile time.
  52. </ul>
  53. </dd>
  54. </dl>
  55. These rules can make it difficult to use casting with a function template argument.
  56. Consider the following example:
  57. <pre><code>
  58. #include &lt;boost/serialization/smart_cast.hpp&gt;
  59. struct top {
  60. };
  61. struct base1 : public top {
  62. bool is_storable() const {
  63. return true;
  64. }
  65. virtual ~base1();
  66. };
  67. struct base2 {
  68. virtual ~base2();
  69. };
  70. struct derived1 :
  71. public base1
  72. {
  73. derived1();
  74. };
  75. struct derived2 :
  76. public base1,
  77. public base2
  78. {
  79. derived2();
  80. };
  81. template&lt;class T&gt;
  82. bool is_storable(T &amp;t){
  83. // what type of cast to use here?
  84. // this fails at compile time when T == base2
  85. // return static_cast&lt;base1 &amp;&gt;(t).is_storable();
  86. // this fails at compile time when T == top
  87. // otherwise it works but cannot optimize inline function call
  88. // return dynamic_cast&lt;base1 &amp;&gt;(t).is_storable();
  89. // this always works - and is guaranteed to generate the fastest code !
  90. return (boost::smart_cast_reference&lt;base1 &amp;&gt;(t)).is_storable();
  91. }
  92. int main(){
  93. derived1 d1;
  94. top &amp; t1 = d1;
  95. derived2 d2;
  96. base2 &amp; b2 = d2;
  97. bool result;
  98. result = is_storable(d1);
  99. result = is_storable(d2);
  100. result = is_storable(b2);
  101. result = is_storable(b2);
  102. result = is_storable(t1);
  103. return 0;
  104. }
  105. </code></pre>
  106. The serialization library includes a mix of classes which use
  107. both static polymorphism (<strong>CRTP</strong>) and dynamic
  108. polymorphism via virtual functions. <code style="white-space: normal">smart_cast</code>
  109. was written to address the more problematic manifestations of the
  110. situation exemplified above.
  111. <h3>Usage</h3>
  112. The following syntax is supported:
  113. <pre><code>
  114. smart_cast&lt;Target *, Source *&gt;(Source * s);
  115. smart_cast&lt;Target *>(Source * s);
  116. smart_cast&lt;Target &, Source &amp;&gt(Source &amp; s);
  117. </code></pre>
  118. Note that the above syntax doesn't include
  119. <pre><code>
  120. smart_cast&lt;Target &amp; &gt(Source &amp; s)
  121. </code></pre>
  122. but the same functionality is supported with the following special syntax
  123. <pre><code>
  124. smart_cast_reference&lt;Target &amp;&gt(Source &amp; s)
  125. </code></pre>
  126. <h3>Requirements</h3>
  127. <code style="white-space: normal">smart_cast</code> can be used only on compilers that support partial
  128. template specialization or on types for which the
  129. macro <code style="white-space: normal">
  130. BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(&lt;type&gt;)</code>
  131. has been applied.
  132. <hr>
  133. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  134. Distributed under the Boost Software License, Version 1.0. (See
  135. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  136. </i></p>
  137. </body>
  138. </html>