2004-01-13 23:03:18 +00:00
<?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" />
2007-07-06 19:47:17 +00:00
< meta name = "generator" content = "Docutils 0.5: http://docutils.sourceforge.net/" />
2004-01-13 23:03:18 +00:00
< title > pointee and indirect_reference</ title >
< meta name = "author" content = "David Abrahams" />
< meta name = "organization" content = "Boost Consulting" />
2007-07-06 19:47:17 +00:00
< meta name = "date" content = "2006-09-11" />
2004-11-02 14:31:27 +00:00
< meta name = "copyright" content = "Copyright David Abrahams 2004." />
2007-07-06 19:47:17 +00:00
< link rel = "stylesheet" href = "../../../rst.css" type = "text/css" />
2004-01-13 23:03:18 +00:00
</ head >
< body >
2005-05-20 15:32:55 +00:00
< div class = "document" id = "pointee-and-indirect-reference" >
< h1 class = "title" >< tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt > and < tt class = "docutils literal" >< span class = "pre" > indirect_reference</ span ></ tt ></ h1 >
2004-01-13 23:03:18 +00:00
< 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 >
2007-07-06 19:47:17 +00:00
< td >< a class = "first last reference external" href = "mailto:dave@boost-consulting.com" > dave@ boost-consulting.com</ a ></ td ></ tr >
2004-01-13 23:03:18 +00:00
< tr >< th class = "docinfo-name" > Organization:</ th >
2007-07-06 19:47:17 +00:00
< td >< a class = "first last reference external" href = "http://www.boost-consulting.com" > Boost Consulting</ a ></ td ></ tr >
2004-01-13 23:03:18 +00:00
< tr >< th class = "docinfo-name" > Date:</ th >
2007-07-06 19:47:17 +00:00
< td > 2006-09-11</ td ></ tr >
2004-01-13 23:03:18 +00:00
< tr >< th class = "docinfo-name" > Copyright:</ th >
2004-11-02 14:31:27 +00:00
< td > Copyright David Abrahams 2004.</ td ></ tr >
2004-01-13 23:03:18 +00:00
</ tbody >
</ table >
2006-09-11 22:27:29 +00:00
<!-- Distributed under 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) -->
2005-05-20 15:32:55 +00:00
< table class = "docutils field-list" frame = "void" rules = "none" >
2004-01-13 23:03:18 +00:00
< 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 >
2007-07-06 19:47:17 +00:00
< div class = "section" id = "overview" >
< h1 > Overview</ h1 >
2004-01-13 23:03:18 +00:00
< 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 >
2007-07-06 19:47:17 +00:00
< div class = "section" id = "pointee" >
< h2 >< tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt ></ h2 >
2004-01-13 23:03:18 +00:00
< 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
2005-05-20 15:32:55 +00:00
possible to require that < tt class = "docutils literal" >< span class = "pre" > pointee< Dereferenceable> ::type</ span ></ tt > is
correct. Naturally, < tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt > has the same difficulty: it can't
determine the appropriate < tt class = "docutils literal" >< span class = "pre" > ::type</ span ></ tt > reliably for all
< tt class = "docutils literal" >< span class = "pre" > Dereferenceable</ span ></ tt > s, but it makes very good guesses (it works
2004-01-13 23:03:18 +00:00
for all pointers, standard and boost smart pointers, and
iterators), and when it guesses wrongly, it can be specialized as
2005-02-27 17:28:24 +00:00
necessary:</ p >
2004-01-13 23:03:18 +00:00
< pre class = "literal-block" >
namespace boost
{
template < class T>
struct pointee< third_party_lib::smart_pointer< T> >
{
typedef T type;
};
}
</ pre >
</ div >
2007-07-06 19:47:17 +00:00
< div class = "section" id = "indirect-reference" >
< h2 >< tt class = "docutils literal" >< span class = "pre" > indirect_reference</ span ></ tt ></ h2 >
2005-05-20 15:32:55 +00:00
< p >< tt class = "docutils literal" >< span class = "pre" > indirect_reference< T> ::type</ span ></ tt > is rather more specialized than
< tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt > , and is meant to be used to forward the result of
2004-01-13 23:03:18 +00:00
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
2005-05-20 15:32:55 +00:00
information is needed, call on < tt class = "docutils literal" >< span class = "pre" > indirect_reference</ span ></ tt > .</ p >
2004-01-13 23:03:18 +00:00
< p > Both of these templates are essential to the correct functioning of
2007-07-06 19:47:17 +00:00
< a class = "reference external" href = "indirect_iterator.html" >< tt class = "docutils literal" >< span class = "pre" > indirect_iterator</ span ></ tt ></ a > .</ p >
2004-01-13 23:03:18 +00:00
</ div >
</ div >
2007-07-06 19:47:17 +00:00
< div class = "section" id = "reference" >
< h1 > Reference</ h1 >
< div class = "section" id = "id1" >
< h2 >< tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt ></ h2 >
2004-01-13 23:03:18 +00:00
<!-- 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 >
2005-05-20 15:32:55 +00:00
< table class = "docutils field-list" frame = "void" rules = "none" >
2004-01-13 23:03:18 +00:00
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
2005-05-20 15:32:55 +00:00
< tr class = "field" >< th class = "field-name" > Requires:</ th >< td class = "field-body" > For an object < tt class = "docutils literal" >< span class = "pre" > x</ span ></ tt > of type < tt class = "docutils literal" >< span class = "pre" > Dereferenceable</ span ></ tt > , < tt class = "docutils literal" >< span class = "pre" > *x</ span ></ tt >
is well-formed. If < tt class = "docutils literal" >< span class = "pre" > ++x</ span ></ tt > is ill-formed it shall neither be
2004-01-13 23:03:18 +00:00
ambiguous nor shall it violate access control, and
2005-05-20 15:32:55 +00:00
< tt class = "docutils literal" >< span class = "pre" > Dereferenceable::element_type</ span ></ tt > shall be an accessible type.
Otherwise < tt class = "docutils literal" >< span class = "pre" > iterator_traits< Dereferenceable> ::value_type</ span ></ tt > shall
2004-01-13 23:03:18 +00:00
be well formed. [Note: These requirements need not apply to
2005-05-20 15:32:55 +00:00
explicit or partial specializations of < tt class = "docutils literal" >< span class = "pre" > pointee</ span ></ tt > ]</ td >
2004-01-13 23:03:18 +00:00
</ tr >
</ tbody >
</ table >
2005-05-20 15:32:55 +00:00
< p >< tt class = "docutils literal" >< span class = "pre" > type</ span ></ tt > is determined according to the following algorithm, where
< tt class = "docutils literal" >< span class = "pre" > x</ span ></ tt > is an object of type < tt class = "docutils literal" >< span class = "pre" > Dereferenceable</ span ></ tt > :</ p >
2004-01-13 23:03:18 +00:00
< 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 >
2007-07-06 19:47:17 +00:00
< div class = "section" id = "id2" >
< h2 >< tt class = "docutils literal" >< span class = "pre" > indirect_reference</ span ></ tt ></ h2 >
2004-01-13 23:03:18 +00:00
<!-- 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 >
2005-05-20 15:32:55 +00:00
< table class = "docutils field-list" frame = "void" rules = "none" >
2004-01-13 23:03:18 +00:00
< col class = "field-name" />
< col class = "field-body" />
< tbody valign = "top" >
2005-05-20 15:32:55 +00:00
< tr class = "field" >< th class = "field-name" > Requires:</ th >< td class = "field-body" > For an object < tt class = "docutils literal" >< span class = "pre" > x</ span ></ tt > of type < tt class = "docutils literal" >< span class = "pre" > Dereferenceable</ span ></ tt > , < tt class = "docutils literal" >< span class = "pre" > *x</ span ></ tt >
is well-formed. If < tt class = "docutils literal" >< span class = "pre" > ++x</ span ></ tt > is ill-formed it shall neither be
2004-01-13 23:03:18 +00:00
ambiguous nor shall it violate access control, and
2005-05-20 15:32:55 +00:00
< tt class = "docutils literal" >< span class = "pre" > pointee< Dereferenceable> ::type& </ span ></ tt > shall be well-formed.
Otherwise < tt class = "docutils literal" >< span class = "pre" > iterator_traits< Dereferenceable> ::reference</ span ></ tt > shall
2004-01-13 23:03:18 +00:00
be well formed. [Note: These requirements need not apply to
2005-05-20 15:32:55 +00:00
explicit or partial specializations of < tt class = "docutils literal" >< span class = "pre" > indirect_reference</ span ></ tt > ]</ td >
2004-01-13 23:03:18 +00:00
</ tr >
</ tbody >
</ table >
2005-05-20 15:32:55 +00:00
< p >< tt class = "docutils literal" >< span class = "pre" > type</ span ></ tt > is determined according to the following algorithm, where
< tt class = "docutils literal" >< span class = "pre" > x</ span ></ tt > is an object of type < tt class = "docutils literal" >< span class = "pre" > Dereferenceable</ span ></ tt > :</ p >
2004-01-13 23:03:18 +00:00
< pre class = "literal-block" >
if ( ++x is ill-formed )
return ``pointee< Dereferenceable> ::type& ``
else
2004-01-14 04:48:53 +00:00
std::iterator_traits< Dereferenceable> ::reference
2004-01-13 23:03:18 +00:00
</ pre >
</ div >
</ div >
2007-07-06 19:47:17 +00:00
</ div >
< div class = "footer" >
< hr class = "footer" />
< a class = "reference external" href = "pointee.rst" > View document source</ a > .
Generated by < a class = "reference external" href = "http://docutils.sourceforge.net/" > Docutils</ a > from < a class = "reference external" href = "http://docutils.sourceforge.net/rst.html" > reStructuredText</ a > source.
2004-01-13 23:03:18 +00:00
</ div >
</ body >
</ html >