1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Function polymorphic_relaxed_get</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../variant/reference.html#header.boost.variant.polymorphic_get_hpp" title="Header <boost/variant/polymorphic_get.hpp>"> 10<link rel="prev" href="bad_polymorphic_get.html" title="Class bad_polymorphic_get"> 11<link rel="next" href="polymorphic_strict_get.html" title="Function polymorphic_strict_get"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="bad_polymorphic_get.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../variant/reference.html#header.boost.variant.polymorphic_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="polymorphic_strict_get.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="refentry"> 27<a name="boost.polymorphic_relaxed_get"></a><div class="titlepage"></div> 28<div class="refnamediv"> 29<h2><span class="refentrytitle">Function polymorphic_relaxed_get</span></h2> 30<p>boost::polymorphic_relaxed_get — Retrieves a value of a specified type from a given 31 <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code>.Unlike <a class="link" href="polymorphic_strict_get.html" title="Function polymorphic_strict_get">polymorphic_strict_get</a> does not assert at compile time 32 that type <code class="computeroutput">U</code> is one of the types that can be stored in variant.</p> 33</div> 34<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> 35<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: <<a class="link" href="../variant/reference.html#header.boost.variant.polymorphic_get_hpp" title="Header <boost/variant/polymorphic_get.hpp>">boost/variant/polymorphic_get.hpp</a>> 36 37</span> 38<span class="keyword">template</span><span class="special"><</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">></span> 39 <span class="identifier">U</span> <span class="special">*</span> <span class="identifier">polymorphic_relaxed_get</span><span class="special">(</span><a class="link" href="variant.html" title="Class template variant">variant</a><span class="special"><</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">></span> <span class="special">*</span> operand<span class="special">)</span><span class="special">;</span> 40<span class="keyword">template</span><span class="special"><</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">></span> 41 <span class="keyword">const</span> <span class="identifier">U</span> <span class="special">*</span> <span class="identifier">polymorphic_relaxed_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"><</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">></span> <span class="special">*</span> operand<span class="special">)</span><span class="special">;</span> 42<span class="keyword">template</span><span class="special"><</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">></span> 43 <span class="identifier">U</span> <span class="special">&</span> <span class="identifier">polymorphic_relaxed_get</span><span class="special">(</span><a class="link" href="variant.html" title="Class template variant">variant</a><span class="special"><</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">></span> <span class="special">&</span> operand<span class="special">)</span><span class="special">;</span> 44<span class="keyword">template</span><span class="special"><</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">></span> 45 <span class="keyword">const</span> <span class="identifier">U</span> <span class="special">&</span> <span class="identifier">polymorphic_relaxed_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"><</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">></span> <span class="special">&</span> operand<span class="special">)</span><span class="special">;</span></pre></div> 46<div class="refsect1"> 47<a name="id-1.3.46.5.11.4.4"></a><h2>Description</h2> 48<p>The <code class="computeroutput">polymorphic_get</code> function allows run-time checked, 49 type-safe retrieval of the content of the given 50 <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code>. The function succeeds 51 only if the content is of the specified type <code class="computeroutput">U</code> or of type 52 derived from type <code class="computeroutput">U</code>, with 53 failure indicated as described below.</p> 54<p><span class="bold"><strong>Recomendation</strong></span>: Use 55 <code class="computeroutput"><a class="link" href="polymorphic_get.html" title="Function polymorphic_get">polymorphic_get</a></code> or <code class="computeroutput"><a class="link" href="polymorphic_strict_get.html" title="Function polymorphic_strict_get">polymorphic_strict_get</a></code> 56 in new code. 57 <code class="computeroutput"><a class="link" href="polymorphic_strict_get.html" title="Function polymorphic_strict_get">polymorphic_strict_get</a></code> 58 provides more compile time checks and its behavior is closer to <code class="computeroutput">std::get</code> 59 from C++ Standard Library.</p> 60<p><span class="bold"><strong>Warning</strong></span>: After either 61 <code class="computeroutput">operand</code> or its content is destroyed (e.g., when the 62 given <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> is assigned a 63 value of different type), the returned reference is invalidated. 64 Thus, significant care and caution must be extended when handling 65 the returned reference.</p> 66<div class="variablelist"><table border="0" class="variablelist compact"> 67<colgroup> 68<col align="left" valign="top"> 69<col> 70</colgroup> 71<tbody> 72<tr> 73<td><p><span class="term">Notes:</span></p></td> 74<td>As part of its guarantee of type-safety, <code class="computeroutput">polymorphic_get</code> 75 enforces <code class="computeroutput">const</code>-correctness. Thus, the specified type 76 <code class="computeroutput">U</code> must be <code class="computeroutput">const</code>-qualified whenever 77 <code class="computeroutput">operand</code> or its content is likewise 78 <code class="computeroutput">const</code>-qualified. The converse, however, is not required: 79 that is, the specified type <code class="computeroutput">U</code> may be 80 <code class="computeroutput">const</code>-qualified even when <code class="computeroutput">operand</code> and its 81 content are not.</td> 82</tr> 83<tr> 84<td><p><span class="term">Returns:</span></p></td> 85<td>If passed a pointer, <code class="computeroutput">polymorphic_get</code> returns a pointer to 86 the value content if it is of the specified type <code class="computeroutput">U</code> or of type 87 derived from type <code class="computeroutput">U</code>; 88 otherwise, a null pointer is returned. If passed a reference, 89 <code class="computeroutput">polymorphic_get</code> returns a reference to the value content if it is of 90 the specified type <code class="computeroutput">U</code> or of type 91 derived from type <code class="computeroutput">U</code>; otherwise, an exception is thrown 92 (see below).</td> 93</tr> 94<tr> 95<td><p><span class="term">Throws:</span></p></td> 96<td>Overloads taking a 97 <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> pointer will not 98 throw; the overloads taking a 99 <code class="computeroutput"><a class="link" href="variant.html" title="Class template variant">variant</a></code> reference throw 100 <code class="computeroutput"><a class="link" href="bad_polymorphic_get.html" title="Class bad_polymorphic_get">bad_polymorphic_get</a></code> if the content is not of 101 the specified type <code class="computeroutput">U</code>or of type 102 derived from type <code class="computeroutput">U</code>.</td> 103</tr> 104<tr> 105<td><p><span class="term">Rationale:</span></p></td> 106<td>While visitation via 107 <code class="computeroutput"><a class="link" href="apply_visitor.html" title="Function apply_visitor">apply_visitor</a></code> 108 is generally preferred due to its greater safety, <code class="computeroutput">polymorphic_get</code> may 109 may be more convenient in some cases due to its straightforward 110 usage.</td> 111</tr> 112</tbody> 113</table></div> 114</div> 115</div> 116<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 117<td align="left"></td> 118<td align="right"><div class="copyright-footer">Copyright © 2002, 2003 Eric Friedman, Itay Maman<br>Copyright © 2014-2020 Antony Polukhin<p>Distributed under the Boost Software License, Version 1.0. 119 (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 120 <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 121 </p> 122</div></td> 123</tr></table> 124<hr> 125<div class="spirit-nav"> 126<a accesskey="p" href="bad_polymorphic_get.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../variant/reference.html#header.boost.variant.polymorphic_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="polymorphic_strict_get.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 127</div> 128</body> 129</html> 130