mirror of
https://github.com/boostorg/functional.git
synced 2026-01-19 04:12:10 +00:00
197 lines
6.2 KiB
HTML
197 lines
6.2 KiB
HTML
<html>
|
|
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<title>Boost Function Object Adapter Library</title>
|
|
</head>
|
|
|
|
<body bgcolor="#FFFFFF" text="#000000">
|
|
|
|
<table border="1" bgcolor="#007F7F" cellpadding="2">
|
|
<tr>
|
|
<td bgcolor="#FFFFFF"><img src="../../boost.png" alt="boost.png (6897 bytes)" WIDTH="277" HEIGHT="86"></td>
|
|
<td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home </big></font></a></td>
|
|
<td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries </big></font></a></td>
|
|
<td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People </big></font></a></td>
|
|
<td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ </big></font></a></td>
|
|
<td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More </big></font></a></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<h1>Function Object Traits</h1>
|
|
|
|
<p>The header <nobr><a
|
|
href="../../boost/functional.hpp">functional.hpp</a></nobr> provides two
|
|
traits class templates for functions and function objects:</p>
|
|
|
|
<table border="1">
|
|
<tr>
|
|
<th>Type</th>
|
|
<th>Contents</th>
|
|
<th>Description</th>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top" rowspan="4"><tt><nobr>template <typename T></nobr><br><nobr>struct unary_traits<nobr></tt>
|
|
</td>
|
|
<td valign="top"><tt><nobr>function_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type of the function or function object itself (i.e., <tt>T</tt>).
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>param_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type that should be used to pass the function or function object as a parameter.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>result_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type returned by the function or function object.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>argument_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type of the argument to the function or function object.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top" rowspan="5"><tt><nobr>template <typename T></nobr><br><nobr>struct binary_traits<nobr></tt>
|
|
</td>
|
|
<td valign="top"><tt><nobr>function_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type of the function or function object itself (i.e., <tt>T</tt>).
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>param_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type that should be used to pass the function or function object as a parameter.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>result_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type returned by the function or function object.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>first_argument_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type of the first argument to the function or function object.
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td valign="top"><tt><nobr>second_argument_type</nobr></tt>
|
|
</td>
|
|
<td valign="top">The type of the second argument to the function or function object.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<h3>Usage</h3>
|
|
|
|
<p><tt><nobr>unary_traits</nobr></tt> should be instantiated with
|
|
either a function taking a single parameter, or an adaptable unary
|
|
function object (i.e., a class derived from
|
|
<tt><nobr>std::unary_function</nobr></tt> or one which provides the
|
|
same typedefs). (See §20.3.1 in the C++ Standard.)
|
|
|
|
<p><tt><nobr>binary_traits</nobr></tt> should be instantiated with
|
|
either a function taking two parameters, or an adaptable binary
|
|
function object (i.e., a class derived from
|
|
<tt><nobr>std::binary_function</nobr></tt> or one which provides the
|
|
same typedefs). (See §20.3.1 in the C++ Standard.)
|
|
|
|
<p>The most common usage of these templates is in function object
|
|
adapters, thus allowing them to adapt plain functions as well as
|
|
function objects. You can do this by wherever you would normally
|
|
write, for example,
|
|
|
|
<blockquote><pre>
|
|
typename Operation::argument_type
|
|
</pre></blockquote>
|
|
|
|
<p>simply writing
|
|
|
|
<blockquote><pre>
|
|
typename boost::unary_traits<Operation>::argument_type
|
|
</pre></blockquote>
|
|
|
|
<p>instead.
|
|
|
|
<h3>Additional Types Defined</h3>
|
|
|
|
<p>In addition to the standard result and argument typedefs, these
|
|
traits templates define two additional types.
|
|
|
|
<h4><tt>function_type</tt></h4>
|
|
|
|
<p>This is the type of the function or function object, and can be
|
|
used in declarations such as</p>
|
|
|
|
<blockquote><pre>
|
|
template <class Predicate>
|
|
class unary_negate : // ...
|
|
{
|
|
// ...
|
|
private:
|
|
<strong>typename unary_traits<Predicate>::function_type</strong> pred;
|
|
};
|
|
</pre></blockquote>
|
|
|
|
<p>If this typedef were not provided, it would not be possible to
|
|
declare <tt>pred</tt> in a way that would allow
|
|
<tt><nobr>unary_negate</nobr></tt> to be instantiated with a function
|
|
type (see the C++ Standard §14.3.1 ¶3).
|
|
|
|
<h4><tt>param_type</tt></h4>
|
|
|
|
<p>This is a type suitable for passing the function or function object
|
|
as a parameter to another function. For example,
|
|
|
|
<blockquote><pre>
|
|
template <class Predicate>
|
|
class unary_negate : // ...
|
|
{
|
|
public:
|
|
explicit unary_negate(<strong>typename unary_traits<Predicate>::param_type</strong> x)
|
|
:
|
|
pred(x)
|
|
{}
|
|
// ...
|
|
};
|
|
</pre></blockquote>
|
|
|
|
<p>Function objects are passed by reference to const; function
|
|
pointers are passed by value.</p>
|
|
|
|
|
|
<h3>Limitations</h3>
|
|
|
|
<p>This library uses these traits within all function object adapters,
|
|
theoretically rendering <tt><nobr>ptr_fun</nobr></tt> obsolete.
|
|
However, third party adapters probably won't take advantage of this
|
|
mechanism, and so <tt><nobr>ptr_fun</nobr></tt> may still be required.
|
|
Accordingly, this library also provides <a
|
|
href="ptr_fun.html">improved versions of the standard function pointer
|
|
adapters</a>.</p>
|
|
|
|
<p>These traits templates will also not work with compilers that fail
|
|
to support partial specialisation of templates. With these compilers,
|
|
the traits templates can only be instantiated with adaptable function
|
|
objects, thus requiring <tt><nobr>ptr_fun</nobr></tt> to be used, even
|
|
with the function object adapters in this library.
|
|
|
|
<hr>
|
|
<p>Copyright © 2000 Cadenza New Zealand Ltd. Permission to copy,
|
|
use, modify, sell and distribute this document is granted provided
|
|
this copyright notice appears in all copies. This document is provided
|
|
"as is" without express or implied warranty, and with no claim as to
|
|
its suitability for any purpose.</p>
|
|
|
|
<p>Revised 28 June 2000</p>
|
|
</body>
|
|
</html>
|