strong_typedef.html 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 style="white-space: normal">BOOST_STRONG_TYPEDEF</code></h2>
  24. </td>
  25. </tr>
  26. </table>
  27. <hr>
  28. <h3>Motivation</h3>
  29. <code style="white-space: normal">typedef</code> creates an alias for an existing type. It does not create
  30. a new type that can be used for matching either function or template parameters.
  31. This can be shown by trying to compile the following example.
  32. <pre></code>
  33. typedef int a;
  34. void f(int x); // (1) function to handle simple integers
  35. void f(a x); // (2) special function to handle integers of type a
  36. int main(){
  37. int x = 1;
  38. a y;
  39. y = x; // other operations permitted as a is converted as necessary
  40. f(x); // chooses (1)
  41. f(y); // chooses (2)
  42. }
  43. </code></pre>
  44. Since typedef doesn't create a new type, this program can't compile to code
  45. that implements its obvious intention.
  46. <p>
  47. Usage of BOOST_STRONG_TYPEDEF
  48. addresses this.
  49. <pre></code>
  50. <a target="strong_typedef" href="../../../boost/strong_typedef.hpp">
  51. #include &lt;boost/serialization/strong_typedef.hpp&gt;
  52. </a>
  53. BOOST_STRONG_TYPEDEF(int, a)
  54. void f(int x); // (1) function to handle simple integers
  55. void f(a x); // (2) special function to handle integers of type a
  56. int main(){
  57. int x = 1;
  58. a y;
  59. y = x; // other operations permitted as a is converted as necessary
  60. f(x); // chooses (1)
  61. f(y); // chooses (2)
  62. }
  63. </code></pre>
  64. The program will now compile and run as expected.
  65. <h3>Usage</h3>
  66. Syntax of <code style="white-space: normal">BOOST_STRONG_TYPEDEF</code>
  67. has been designed to be similar to the standard
  68. <code style="white-space: normal">typedef</code>. So
  69. <pre><code>
  70. BOOST_STRONG_TYPEDEF(primitive type, name)
  71. </code></pre>
  72. will create a new type "name" which will be substitutable for the original
  73. type but still of distinct type.
  74. <h3>Implemenation</h3>
  75. <code style="white-space: normal">BOOST_STRONG_TYPEDEF</code> is a macro
  76. which generates a class named "name" which wraps an instance of its
  77. primitive type and provides appropriate conversion operators in order
  78. to make the new type substitutable for the one that it wraps.
  79. <hr>
  80. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  81. Distributed under the Boost Software License, Version 1.0. (See
  82. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  83. </i></p>
  84. </body>
  85. </html>