<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="generator" content="Docutils 0.3.1: http://docutils.sourceforge.net/" /> <title>pointee and indirect_reference</title> <meta name="author" content="David Abrahams" /> <meta name="organization" content="Boost Consulting" /> <meta name="date" content="2004-01-29" /> <meta name="copyright" content="Copyright David Abrahams 2004. All rights reserved" /> <link rel="stylesheet" href="default.css" type="text/css" /> </head> <body> <div class="document" id="pointee-and-indirect-reference"> <h1 class="title"><tt class="literal"><span class="pre">pointee</span></tt> and <tt class="literal"><span class="pre">indirect_reference</span></tt></h1> <table class="docinfo" frame="void" rules="none"> <col class="docinfo-name" /> <col class="docinfo-content" /> <tbody valign="top"> <tr><th class="docinfo-name">Author:</th> <td>David Abrahams</td></tr> <tr><th class="docinfo-name">Contact:</th> <td><a class="first reference" href="mailto:dave@boost-consulting.com">dave@boost-consulting.com</a>, <a class="reference" href="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>, <a class="last reference" href="mailto:witt@ive.uni-hannover.de">witt@ive.uni-hannover.de</a></td></tr> <tr><th class="docinfo-name">Organization:</th> <td><a class="first last reference" href="http://www.boost-consulting.com">Boost Consulting</a></td></tr> <tr><th class="docinfo-name">Date:</th> <td>2004-01-29</td></tr> <tr><th class="docinfo-name">Copyright:</th> <td>Copyright David Abrahams 2004. All rights reserved</td></tr> </tbody> </table> <table class="field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field"><th class="field-name">abstract:</th><td class="field-body">Provides the capability to deduce the referent types of pointers, smart pointers and iterators in generic code.</td> </tr> </tbody> </table> <div class="section" id="overview"> <h1><a name="overview">Overview</a></h1> <p>Have you ever wanted to write a generic function that can operate on any kind of dereferenceable object? If you have, you've probably run into the problem of how to determine the type that the object "points at":</p> <pre class="literal-block"> template <class Dereferenceable> void f(Dereferenceable p) { <em>what-goes-here?</em> value = *p; ... } </pre> <div class="section" id="pointee"> <h2><a name="pointee"><tt class="literal"><span class="pre">pointee</span></tt></a></h2> <p>It turns out to be impossible to come up with a fully-general algorithm to do determine <em>what-goes-here</em> directly, but it is possible to require that <tt class="literal"><span class="pre">pointee<Dereferenceable>::type</span></tt> is correct. Naturally, <tt class="literal"><span class="pre">pointee</span></tt> has the same difficulty: it can't determine the appropriate <tt class="literal"><span class="pre">::type</span></tt> reliably for all <tt class="literal"><span class="pre">Dereferenceable</span></tt>s, but it makes very good guesses (it works for all pointers, standard and boost smart pointers, and iterators), and when it guesses wrongly, it can be specialized as neccessary:</p> <pre class="literal-block"> namespace boost { template <class T> struct pointee<third_party_lib::smart_pointer<T> > { typedef T type; }; } </pre> </div> <div class="section" id="indirect-reference"> <h2><a name="indirect-reference"><tt class="literal"><span class="pre">indirect_reference</span></tt></a></h2> <p><tt class="literal"><span class="pre">indirect_reference<T>::type</span></tt> is rather more specialized than <tt class="literal"><span class="pre">pointee</span></tt>, and is meant to be used to forward the result of dereferencing an object of its argument type. Most dereferenceable types just return a reference to their pointee, but some return proxy references or return the pointee by value. When that information is needed, call on <tt class="literal"><span class="pre">indirect_reference</span></tt>.</p> <p>Both of these templates are essential to the correct functioning of <a class="reference" href="indirect_iterator.html"><tt class="literal"><span class="pre">indirect_iterator</span></tt></a>.</p> </div> </div> <div class="section" id="reference"> <h1><a name="reference">Reference</a></h1> <div class="section" id="id1"> <h2><a name="id1"><tt class="literal"><span class="pre">pointee</span></tt></a></h2> <!-- Copyright David Abrahams 2004. Use, modification and distribution is --> <!-- subject to the Boost Software License, Version 1.0. (See accompanying --> <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <pre class="literal-block"> template <class Dereferenceable> struct pointee { typedef /* see below */ type; }; </pre> <table class="field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field"><th class="field-name">Requires:</th><td class="field-body">For an object <tt class="literal"><span class="pre">x</span></tt> of type <tt class="literal"><span class="pre">Dereferenceable</span></tt>, <tt class="literal"><span class="pre">*x</span></tt> is well-formed. If <tt class="literal"><span class="pre">++x</span></tt> is ill-formed it shall neither be ambiguous nor shall it violate access control, and <tt class="literal"><span class="pre">Dereferenceable::element_type</span></tt> shall be an accessible type. Otherwise <tt class="literal"><span class="pre">iterator_traits<Dereferenceable>::value_type</span></tt> shall be well formed. [Note: These requirements need not apply to explicit or partial specializations of <tt class="literal"><span class="pre">pointee</span></tt>]</td> </tr> </tbody> </table> <p><tt class="literal"><span class="pre">type</span></tt> is determined according to the following algorithm, where <tt class="literal"><span class="pre">x</span></tt> is an object of type <tt class="literal"><span class="pre">Dereferenceable</span></tt>:</p> <pre class="literal-block"> if ( ++x is ill-formed ) { return ``Dereferenceable::element_type`` } else if (``*x`` is a mutable reference to std::iterator_traits<Dereferenceable>::value_type) { return iterator_traits<Dereferenceable>::value_type } else { return iterator_traits<Dereferenceable>::value_type const } </pre> </div> <div class="section" id="id2"> <h2><a name="id2"><tt class="literal"><span class="pre">indirect_reference</span></tt></a></h2> <!-- Copyright David Abrahams 2004. Use, modification and distribution is --> <!-- subject to the Boost Software License, Version 1.0. (See accompanying --> <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <pre class="literal-block"> template <class Dereferenceable> struct indirect_reference { typedef /* see below */ type; }; </pre> <table class="field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field"><th class="field-name">Requires:</th><td class="field-body">For an object <tt class="literal"><span class="pre">x</span></tt> of type <tt class="literal"><span class="pre">Dereferenceable</span></tt>, <tt class="literal"><span class="pre">*x</span></tt> is well-formed. If <tt class="literal"><span class="pre">++x</span></tt> is ill-formed it shall neither be ambiguous nor shall it violate access control, and <tt class="literal"><span class="pre">pointee<Dereferenceable>::type&</span></tt> shall be well-formed. Otherwise <tt class="literal"><span class="pre">iterator_traits<Dereferenceable>::reference</span></tt> shall be well formed. [Note: These requirements need not apply to explicit or partial specializations of <tt class="literal"><span class="pre">indirect_reference</span></tt>]</td> </tr> </tbody> </table> <p><tt class="literal"><span class="pre">type</span></tt> is determined according to the following algorithm, where <tt class="literal"><span class="pre">x</span></tt> is an object of type <tt class="literal"><span class="pre">Dereferenceable</span></tt>:</p> <pre class="literal-block"> if ( ++x is ill-formed ) return ``pointee<Dereferenceable>::type&`` else std::iterator_traits<Dereferenceable>::reference </pre> </div> </div> </div> <hr class="footer" /> <div class="footer"> <a class="reference" href="pointee.rst">View document source</a>. Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source. </div> </body> </html>