![]() |
Home | Libraries | People | FAQ | More |
Improved Function Object Adapters
The header
- We use the Boost
call_traits templates to avoid the problem of references to references, and to improve the efficiency of parameter passing. - We use two function object traits class
templates to avoid the need for
ptr_fun with the adapters in this library.
Contents
The header contains the following function and class templates:
| Function object traits | Used to determine the types of function objects' and
functions' arguments. Eliminate the necessity for |
|
|---|---|---|
| Negators | Based on section 20.3.5 of the standard. | |
| Binders | Based on section 20.3.6 of the standard. | |
| Adapters for pointers to functions | Based on section 20.3.7 of the standard. Not required for use with this library since the binders and negators can adapt functions, but may be needed with third party adapters. | |
| Adapters for pointers to member functions | Based on section 20.3.8 of the standard. |
Usage
Using these adapters should be pretty much the same as using the standard
function object adapters; the only differences are that you need to write
For example, suppose you had a Person class that contained a
class Person
{
public:
void set_name(const std::string &name);
// ...
};
You could rename a bunch of people in a collection, c, by writing
std::for_each(c.begin(), c.end(),
boost::bind2nd(boost::mem_fun_ref(&Person::set_name), "Fred"));
If the standard adapters had been used instead then this code would normally
fail to compile, because
Compiler Compatibility
The header and test program have been compiled with the following compilers:
| Compiler | Comments |
|---|---|
| Borland C++Builder 4 Update 2 | No known issues. |
| Borland C++ 5.5 | No known issues. |
| g++ 2.95.2 | No known issues. |
| Microsoft Visual C++ Service Pack 3 | Compiler lacks partial specialisation, so this library
offers little more than is provided by the standard adapters:
|
Future Directions
This library's primary focus is to solve the problem of references to references while maintaining as much compatibility as possible with the standard library. This allows you to use the techniques you read about in books and magazines with many of today's compilers.
In the longer term, even better solutions are likely:
- Several Boost members are working on expression template libraries. These will allow a more natural syntax for combining and adapting functions. As this is a new technology, it may be some time before it has matured and is widely supported by major compilers but shows great promise. In the meantime, the functional.hpp library fills the gap.
- The Standard Committee has recognised the problem of references to references occurring during template instantiation and has moved to fix the standard (see the C++ standard core language active issues list).
Author
Acknowledgements
Thanks to John Maddock for suggesting the mechanism that allowed the function objects traits to work correctly. Jens Maurer provided invaluable feedback during the formal review process.
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.
Revised 28 June 2000

