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>Class template weak_ptr</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="../../interprocess/indexes_reference.html#header.boost.interprocess.smart_ptr.weak_ptr_hpp" title="Header <boost/interprocess/smart_ptr/weak_ptr.hpp>"> 10<link rel="prev" href="managed_weak_ptr.html" title="Struct template managed_weak_ptr"> 11<link rel="next" href="make_managed_weak_ptr.html" title="Function template make_managed_weak_ptr"> 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="managed_weak_ptr.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../interprocess/indexes_reference.html#header.boost.interprocess.smart_ptr.weak_ptr_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="make_managed_weak_ptr.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="refentry"> 27<a name="boost.interprocess.weak_ptr"></a><div class="titlepage"></div> 28<div class="refnamediv"> 29<h2><span class="refentrytitle">Class template weak_ptr</span></h2> 30<p>boost::interprocess::weak_ptr</p> 31</div> 32<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2> 33<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="../../interprocess/indexes_reference.html#header.boost.interprocess.smart_ptr.weak_ptr_hpp" title="Header <boost/interprocess/smart_ptr/weak_ptr.hpp>">boost/interprocess/smart_ptr/weak_ptr.hpp</a>> 34 35</span><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="keyword">typename</span> A<span class="special">,</span> <span class="keyword">typename</span> D<span class="special">></span> 36<span class="keyword">class</span> <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> <span class="special">{</span> 37<span class="keyword">public</span><span class="special">:</span> 38 <span class="comment">// types</span> 39 <span class="keyword">typedef</span> <span class="identifier">T</span> <a name="boost.interprocess.weak_ptr.element_type"></a><span class="identifier">element_type</span><span class="special">;</span> 40 <span class="keyword">typedef</span> <span class="identifier">T</span> <a name="boost.interprocess.weak_ptr.value_type"></a><span class="identifier">value_type</span><span class="special">;</span> 41 42 <span class="comment">// <a class="link" href="weak_ptr.html#boost.interprocess.weak_ptrconstruct-copy-destruct">construct/copy/destruct</a></span> 43 <a class="link" href="weak_ptr.html#idm45304010692128-bb"><span class="identifier">weak_ptr</span></a><span class="special">(</span><span class="special">)</span><span class="special">;</span> 44 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html#idm45304010690656-bb"><span class="identifier">weak_ptr</span></a><span class="special">(</span><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span><span class="special">)</span><span class="special">;</span> 45 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html#idm45304010685936-bb"><span class="identifier">weak_ptr</span></a><span class="special">(</span><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span><span class="special">)</span><span class="special">;</span> 46 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> <span class="special">&</span> <a class="link" href="weak_ptr.html#idm45304010681216-bb"><span class="keyword">operator</span><span class="special">=</span></a><span class="special">(</span><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span><span class="special">)</span><span class="special">;</span> 47 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> <span class="special">&</span> <a class="link" href="weak_ptr.html#idm45304010677392-bb"><span class="keyword">operator</span><span class="special">=</span></a><span class="special">(</span><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span><span class="special">)</span><span class="special">;</span> 48 49 <span class="comment">// <a class="link" href="weak_ptr.html#idm45304010703760-bb">public member functions</a></span> 50 <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">T</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <a class="link" href="weak_ptr.html#idm45304010703200-bb"><span class="identifier">lock</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 51 <span class="keyword">long</span> <a class="link" href="weak_ptr.html#idm45304010701040-bb"><span class="identifier">use_count</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 52 <span class="keyword">bool</span> <a class="link" href="weak_ptr.html#idm45304010698032-bb"><span class="identifier">expired</span></a><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 53 <span class="keyword">void</span> <a class="link" href="weak_ptr.html#idm45304010695872-bb"><span class="identifier">reset</span></a><span class="special">(</span><span class="special">)</span><span class="special">;</span> 54 <span class="keyword">void</span> <a class="link" href="weak_ptr.html#idm45304010694576-bb"><span class="identifier">swap</span></a><span class="special">(</span><span class="identifier">this_type</span> <span class="special">&</span><span class="special">)</span><span class="special">;</span> 55<span class="special">}</span><span class="special">;</span></pre></div> 56<div class="refsect1"> 57<a name="id-1.3.19.17.3.54.5.4"></a><h2>Description</h2> 58<p>The <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> class template stores a "weak reference" to an object that's already managed by a <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a>. To access the object, a <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> can be converted to a <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a> using the <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a> constructor or the member function lock. When the last <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a> to the object goes away and the object is deleted, the attempt to obtain a <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a> from the <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> instances that refer to the deleted object will fail: the constructor will throw an exception of type bad_weak_ptr, and weak_ptr::lock will return an empty <a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a>.</p> 59<p>Every <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> meets the CopyConstructible and Assignable requirements of the C++ Standard Library, and so can be used in standard library containers. Comparison operators are supplied so that <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> works with the standard library's associative containers.</p> 60<p><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> operations never throw exceptions.</p> 61<p>The class template is parameterized on T, the type of the object pointed to. </p> 62<div class="refsect2"> 63<a name="id-1.3.19.17.3.54.5.4.6"></a><h3> 64<a name="boost.interprocess.weak_ptrconstruct-copy-destruct"></a><code class="computeroutput">weak_ptr</code> 65 public 66 construct/copy/destruct</h3> 67<div class="orderedlist"><ol class="orderedlist" type="1"> 68<li class="listitem"> 69<pre class="literallayout"><a name="idm45304010692128-bb"></a><span class="identifier">weak_ptr</span><span class="special">(</span><span class="special">)</span><span class="special">;</span></pre> 70<p>Effects: Constructs an empty <code class="computeroutput"><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a></code>. Postconditions: use_count() == 0. </p> 71</li> 72<li class="listitem"> 73<pre class="literallayout"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a name="idm45304010690656-bb"></a><span class="identifier">weak_ptr</span><span class="special">(</span><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span> r<span class="special">)</span><span class="special">;</span></pre> 74<p>Effects: If r is empty, constructs an empty <code class="computeroutput"><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a></code>; otherwise, constructs a <code class="computeroutput"><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a></code> that shares ownership with r as if by storing a copy of the pointer stored in r.</p> 75<p>Postconditions: use_count() == r.use_count().</p> 76<p>Throws: nothing. </p> 77</li> 78<li class="listitem"> 79<pre class="literallayout"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a name="idm45304010685936-bb"></a><span class="identifier">weak_ptr</span><span class="special">(</span><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span> r<span class="special">)</span><span class="special">;</span></pre> 80<p>Effects: If r is empty, constructs an empty <code class="computeroutput"><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a></code>; otherwise, constructs a <code class="computeroutput"><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a></code> that shares ownership with r as if by storing a copy of the pointer stored in r.</p> 81<p>Postconditions: use_count() == r.use_count().</p> 82<p>Throws: nothing. </p> 83</li> 84<li class="listitem"> 85<pre class="literallayout"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> <span class="special">&</span> <a name="idm45304010681216-bb"></a><span class="keyword">operator</span><span class="special">=</span><span class="special">(</span><a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span> r<span class="special">)</span><span class="special">;</span></pre> 86<p>Effects: Equivalent to weak_ptr(r).swap(*this).</p> 87<p>Throws: nothing.</p> 88<p>Notes: The implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary. </p> 89</li> 90<li class="listitem"> 91<pre class="literallayout"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> Y<span class="special">></span> <a class="link" href="weak_ptr.html" title="Class template weak_ptr">weak_ptr</a> <span class="special">&</span> <a name="idm45304010677392-bb"></a><span class="keyword">operator</span><span class="special">=</span><span class="special">(</span><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">Y</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <span class="keyword">const</span> <span class="special">&</span> r<span class="special">)</span><span class="special">;</span></pre> 92<p>Effects: Equivalent to weak_ptr(r).swap(*this).</p> 93<p>Throws: nothing.</p> 94<p>Notes: The implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary. </p> 95</li> 96</ol></div> 97</div> 98<div class="refsect2"> 99<a name="id-1.3.19.17.3.54.5.4.7"></a><h3> 100<a name="idm45304010703760-bb"></a><code class="computeroutput">weak_ptr</code> public member functions</h3> 101<div class="orderedlist"><ol class="orderedlist" type="1"> 102<li class="listitem"> 103<pre class="literallayout"><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a><span class="special"><</span> <span class="identifier">T</span><span class="special">,</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">D</span> <span class="special">></span> <a name="idm45304010703200-bb"></a><span class="identifier">lock</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre> 104<p>Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).</p> 105<p>Throws: nothing. </p> 106</li> 107<li class="listitem"> 108<pre class="literallayout"><span class="keyword">long</span> <a name="idm45304010701040-bb"></a><span class="identifier">use_count</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre> 109<p>Returns: 0 if *this is empty; otherwise, the number of <code class="computeroutput"><a class="link" href="shared_ptr.html" title="Class template shared_ptr">shared_ptr</a></code> objects that share ownership with *this.</p> 110<p>Throws: nothing.</p> 111<p>Notes: use_count() is not necessarily efficient. Use only for debugging and testing purposes, not for production code. </p> 112</li> 113<li class="listitem"> 114<pre class="literallayout"><span class="keyword">bool</span> <a name="idm45304010698032-bb"></a><span class="identifier">expired</span><span class="special">(</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span></pre> 115<p>Returns: Returns: use_count() == 0.</p> 116<p>Throws: nothing.</p> 117<p>Notes: expired() may be faster than use_count(). </p> 118</li> 119<li class="listitem"> 120<pre class="literallayout"><span class="keyword">void</span> <a name="idm45304010695872-bb"></a><span class="identifier">reset</span><span class="special">(</span><span class="special">)</span><span class="special">;</span></pre> 121<p>Effects: Equivalent to: weak_ptr().swap(*this). </p> 122</li> 123<li class="listitem"> 124<pre class="literallayout"><span class="keyword">void</span> <a name="idm45304010694576-bb"></a><span class="identifier">swap</span><span class="special">(</span><span class="identifier">this_type</span> <span class="special">&</span> other<span class="special">)</span><span class="special">;</span></pre> 125<p>Effects: Exchanges the contents of the two smart pointers.</p> 126<p>Throws: nothing. </p> 127</li> 128</ol></div> 129</div> 130</div> 131</div> 132<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 133<td align="left"></td> 134<td align="right"><div class="copyright-footer">Copyright © 2005-2015 Ion Gaztanaga<p> 135 Distributed under the Boost Software License, Version 1.0. (See accompanying 136 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 137 </p> 138</div></td> 139</tr></table> 140<hr> 141<div class="spirit-nav"> 142<a accesskey="p" href="managed_weak_ptr.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../interprocess/indexes_reference.html#header.boost.interprocess.smart_ptr.weak_ptr_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="make_managed_weak_ptr.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 143</div> 144</body> 145</html> 146