blob: 5c1ee088fc4c639f7a90bdcff8bf3a6ca49c6e8b [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Function get</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../variant/reference.html#header.boost.variant.get_hpp" title="Header &lt;boost/variant/get.hpp&gt;">
<link rel="prev" href="bad_get.html" title="Class bad_get">
<link rel="next" href="bad_visit.html" title="Class bad_visit">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bad_get.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../variant/reference.html#header.boost.variant.get_hpp"><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="bad_visit.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="refentry">
<a name="boost.get_id1380826"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2><span class="refentrytitle">Function get</span></h2>
<p>boost::get &#8212; Retrieves a value of a specified type from a given
<code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code>.</p>
</div>
<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2>
<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../variant/reference.html#header.boost.variant.get_hpp" title="Header &lt;boost/variant/get.hpp&gt;">boost/variant/get.hpp</a>&gt;
</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> U<span class="special">,</span> <span class="keyword">typename</span> T1<span class="special">,</span> <span class="keyword">typename</span> T2<span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="keyword">typename</span> TN<span class="special">&gt;</span>
<span class="identifier">U</span> <span class="special">*</span> <span class="identifier">get</span><span class="special">(</span><a class="link" href="variant.html" title="Class template variant">variant</a><span class="special">&lt;</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="identifier">TN</span><span class="special">&gt;</span> <span class="special">*</span> operand<span class="special">)</span><span class="special">;</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> U<span class="special">,</span> <span class="keyword">typename</span> T1<span class="special">,</span> <span class="keyword">typename</span> T2<span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="keyword">typename</span> TN<span class="special">&gt;</span>
<span class="keyword">const</span> <span class="identifier">U</span> <span class="special">*</span> <span class="identifier">get</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="variant.html" title="Class template variant">variant</a><span class="special">&lt;</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="identifier">TN</span><span class="special">&gt;</span> <span class="special">*</span> operand<span class="special">)</span><span class="special">;</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> U<span class="special">,</span> <span class="keyword">typename</span> T1<span class="special">,</span> <span class="keyword">typename</span> T2<span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="keyword">typename</span> TN<span class="special">&gt;</span>
<span class="identifier">U</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">(</span><a class="link" href="variant.html" title="Class template variant">variant</a><span class="special">&lt;</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="identifier">TN</span><span class="special">&gt;</span> <span class="special">&amp;</span> operand<span class="special">)</span><span class="special">;</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> U<span class="special">,</span> <span class="keyword">typename</span> T1<span class="special">,</span> <span class="keyword">typename</span> T2<span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="keyword">typename</span> TN<span class="special">&gt;</span>
<span class="keyword">const</span> <span class="identifier">U</span> <span class="special">&amp;</span> <span class="identifier">get</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="variant.html" title="Class template variant">variant</a><span class="special">&lt;</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...</span><span class="special">,</span> <span class="identifier">TN</span><span class="special">&gt;</span> <span class="special">&amp;</span> operand<span class="special">)</span><span class="special">;</span></pre></div>
<div class="refsect1">
<a name="id3087317"></a><h2>Description</h2>
<p>The <code class="computeroutput">get</code> function allows run-time checked,
type-safe retrieval of the content of the given
<code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code>. The function succeeds
only if the content is of the specified type <code class="computeroutput">U</code>, with
failure indicated as described below.</p>
<p><span class="bold"><strong>Warning</strong></span>: After either
<code class="computeroutput">operand</code> or its content is destroyed (e.g., when the
given <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> is assigned a
value of different type), the returned reference is invalidated.
Thus, significant care and caution must be extended when handling
the returned reference.</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term">Notes:</span></p></td>
<td>As part of its guarantee of type-safety, <code class="computeroutput">get</code>
enforces <code class="computeroutput">const</code>-correctness. Thus, the specified type
<code class="computeroutput">U</code> must be <code class="computeroutput">const</code>-qualified whenever
<code class="computeroutput">operand</code> or its content is likewise
<code class="computeroutput">const</code>-qualified. The converse, however, is not required:
that is, the specified type <code class="computeroutput">U</code> may be
<code class="computeroutput">const</code>-qualified even when <code class="computeroutput">operand</code> and its
content are not.</td>
</tr>
<tr>
<td><p><span class="term">Returns:</span></p></td>
<td>If passed a pointer, <code class="computeroutput">get</code> returns a pointer to
the value content if it is of the specified type <code class="computeroutput">U</code>;
otherwise, a null pointer is returned. If passed a reference,
<code class="computeroutput">get</code> returns a reference to the value content if it is of
the specified type <code class="computeroutput">U</code>; otherwise, an exception is thrown
(see below).</td>
</tr>
<tr>
<td><p><span class="term">Throws:</span></p></td>
<td>Overloads taking a
<code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> pointer will not
throw; the overloads taking a
<code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> reference throw
<code class="computeroutput"><a class="link" href="bad_get.html" title="Class bad_get">bad_get</a></code> if the content is not of
the specified type <code class="computeroutput">U</code>.</td>
</tr>
<tr>
<td><p><span class="term">Rationale:</span></p></td>
<td>While visitation via
<code class="computeroutput"><a class="link" href="apply_visitor.html" title="Function apply_visitor">apply_visitor</a></code>
is generally preferred due to its greater safety, <code class="computeroutput">get</code> may
may be more convenient in some cases due to its straightforward
usage.</td>
</tr>
</tbody>
</table></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2002, 2003 Eric Friedman, Itay Maman<p>Distributed under the Boost Software License, Version 1.0.
(See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at
<a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bad_get.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../variant/reference.html#header.boost.variant.get_hpp"><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="bad_visit.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>