1 <!doctype HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
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)
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>
15 <body link=
"#0000ff" vlink=
"#800080">
16 <table border=
"0" cellpadding=
"7" cellspacing=
"0" width=
"100%" summary=
"header">
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>
22 <h1 align=
"center">Serialization
</h1>
23 <h2 align=
"center"><code>smart_cast
</code></h2>
29 To cast from one type to another related type, C++ provides the following
33 <dt><code>static_cast
<T *<
>(U *)
<br>static_cast
<T &<
>(U &)
</code></dt>
36 <li>required if neither T nor U are polymorphic
37 <li>permitted in other cases.
38 <li>fails to detect erroneous casts of polymophic pointers/references at runtime.
39 <li>does not permit
"cross casting"
40 <li>inline function calls can be optimized away during compile time.
44 <dt><code>dynamic_cast
<T *<
>(U *)
<br>dynamic_cast
<T &<
>(U &)
</code></dt>
47 <li>permitted if either T or U are polymorphic
48 <li>prohibited in other cases.
49 <li>throws exception on detecting erroneous casts of polymorphic pointers/references
51 <li>permits
"cross casting"
52 <li>cannot optimise inline virtual functions at compile time.
57 These rules can make it difficult to use casting with a function template argument.
58 Consider the following example:
61 #include
<boost/serialization/smart_cast.hpp
>
66 struct base1 : public top {
67 bool is_storable() const {
90 template
<class T
>
91 bool is_storable(T
&t){
92 // what type of cast to use here?
94 // this fails at compile time when T == base2
95 // return static_cast
<base1
&>(t).is_storable();
97 // this fails at compile time when T == top
98 // otherwise it works but cannot optimize inline function call
99 // return dynamic_cast
<base1
&>(t).is_storable();
101 // this always works - and is guaranteed to generate the fastest code !
102 return (boost::smart_cast_reference
<base1
&>(t)).is_storable();
112 result = is_storable(d1);
113 result = is_storable(d2);
114 result = is_storable(b2);
115 result = is_storable(b2);
116 result = is_storable(t1);
120 The serialization library includes a mix of classes which use
121 both static polymorphism (
<strong>CRTP
</strong>) and dynamic
122 polymorphism via virtual functions.
<code style=
"white-space: normal">smart_cast
</code>
123 was written to address the more problematic manifestations of the
124 situation exemplified above.
127 The following syntax is supported:
129 smart_cast
<Target *, Source *
>(Source * s);
130 smart_cast
<Target *
>(Source * s);
131 smart_cast
<Target &, Source
&>(Source
& s);
133 Note that the above syntax doesn't include
135 smart_cast
<Target
& >(Source
& s)
137 but the same functionality is supported with the following special syntax
139 smart_cast_reference
<Target
&>(Source
& s)
142 <h3>Requirements
</h3>
143 <code style=
"white-space: normal">smart_cast
</code> can be used only on compilers that support partial
144 template specialization or on types for which the
145 macro
<code style=
"white-space: normal">
146 BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(
<type
>)
</code>
150 <p><i>© Copyright
<a href=
"http://www.rrsd.com">Robert Ramey
</a> 2002-
2004.
151 Distributed under the Boost Software License, Version
1.0. (See
152 accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)