1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE header PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
3 "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4 <header name="boost/variant/get.hpp">
5 <namespace name="boost">
8 <inherit access="public">
9 <classname>std::exception</classname>
13 <simpara>The exception thrown in the event of a failed application of
14 <code><functionname>boost::get</functionname></code> on the given
15 operand value.</simpara>
18 <method name="what" specifiers="virtual" cv="const">
19 <type>const char *</type>
23 <overloaded-function name="get">
26 <template-type-parameter name="U"/>
27 <template-type-parameter name="T1"/>
28 <template-type-parameter name="T2"/>
30 <template-type-parameter name="TN"/>
35 <parameter name="operand">
36 <paramtype><classname>variant</classname><T1, T2, ..., TN> *</paramtype>
42 <template-type-parameter name="U"/>
43 <template-type-parameter name="T1"/>
44 <template-type-parameter name="T2"/>
46 <template-type-parameter name="TN"/>
49 <type>const U *</type>
51 <parameter name="operand">
52 <paramtype>const <classname>variant</classname><T1, T2, ..., TN> *</paramtype>
58 <template-type-parameter name="U"/>
59 <template-type-parameter name="T1"/>
60 <template-type-parameter name="T2"/>
62 <template-type-parameter name="TN"/>
67 <parameter name="operand">
68 <paramtype><classname>variant</classname><T1, T2, ..., TN> &</paramtype>
74 <template-type-parameter name="U"/>
75 <template-type-parameter name="T1"/>
76 <template-type-parameter name="T2"/>
78 <template-type-parameter name="TN"/>
81 <type>const U &</type>
83 <parameter name="operand">
84 <paramtype>const <classname>variant</classname><T1, T2, ..., TN> &</paramtype>
89 <simpara>Retrieves a value of a specified type from a given
90 <code><classname>variant</classname></code>.</simpara>
94 <simpara>The <code>get</code> function allows run-time checked,
95 type-safe retrieval of the content of the given
96 <code><classname>variant</classname></code>. The function succeeds
97 only if the content is of the specified type <code>U</code>, with
98 failure indicated as described below.</simpara>
99 <simpara><emphasis role="bold">Warning</emphasis>: After either
100 <code>operand</code> or its content is destroyed (e.g., when the
101 given <code><classname>variant</classname></code> is assigned a
102 value of different type), the returned reference is invalidated.
103 Thus, significant care and caution must be extended when handling
104 the returned reference.</simpara>
108 <simpara>As part of its guarantee of type-safety, <code>get</code>
109 enforces <code>const</code>-correctness. Thus, the specified type
110 <code>U</code> must be <code>const</code>-qualified whenever
111 <code>operand</code> or its content is likewise
112 <code>const</code>-qualified. The converse, however, is not required:
113 that is, the specified type <code>U</code> may be
114 <code>const</code>-qualified even when <code>operand</code> and its
115 content are not.</simpara>
119 <simpara>If passed a pointer, <code>get</code> returns a pointer to
120 the value content if it is of the specified type <code>U</code>;
121 otherwise, a null pointer is returned. If passed a reference,
122 <code>get</code> returns a reference to the value content if it is of
123 the specified type <code>U</code>; otherwise, an exception is thrown
124 (see below).</simpara>
128 <simpara>Overloads taking a
129 <code><classname>variant</classname></code> pointer will not
130 throw; the overloads taking a
131 <code><classname>variant</classname></code> reference throw
132 <code><classname>bad_get</classname></code> if the content is not of
133 the specified type <code>U</code>.</simpara>
137 <simpara>While visitation via
138 <code><functionname>apply_visitor</functionname></code>
139 is generally preferred due to its greater safety, <code>get</code> may
140 may be more convenient in some cases due to its straightforward
143 </overloaded-function>