diff --git a/binders.html b/binders.html new file mode 100644 index 0000000..172f299 --- /dev/null +++ b/binders.html @@ -0,0 +1,132 @@ + + +
+ +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
As well as the corresponding helper functions
+ +The key benefit of these adapters over those in the Standard +Library is they avoid the problem of references to +references. + +
Usage is identical to the standard binders. For example,
+ ++ ++class Foo { +public: + void bar(std::ostream &); + // ... +}; +// ... +std::vector<Foo> c; +// ... +std::for_each(c.begin(), c.end(), + boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout)); +
Consider the usage example above
+ ++ ++class Foo { +public: + void bar(std::ostream &); + // ... +}; +// ... +std::for_each(c.begin(), c.end(), + boost::bind2nd(boost::mem_fun_ref(&Foo::bar), std::cout)); +
If this had been written using
The problem arises because
The call to
+ ++template <class Operation> +class binder2nd + : public unary_function<typename Operation::first_argument_type, + typename Operation::result_type> { +... +public: + binder2nd(const Operation& x, + const typename Operation::second_argument_type& y); + ... +
Since our operation's
The binders in this library avoid this problem by using the Boost
+
Our constructor is declared + +
+ ++binder2nd(const Operation& x, + typename call_traits< + typename binary_traits<Operation>::second_argument_type + >::param_type y) +
As a result, y has a type of
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
+ + + diff --git a/function_traits.html b/function_traits.html new file mode 100644 index 0000000..59e11d9 --- /dev/null +++ b/function_traits.html @@ -0,0 +1,196 @@ + + + + +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
Type | +Contents | +Description | +
---|---|---|
The type of the function or function object itself (i.e., T). + | +||
The type that should be used to pass the function or function object as a parameter. + | +||
The type returned by the function or function object. + | +||
The type of the argument to the function or function object. + | +||
The type of the function or function object itself (i.e., T). + | +||
The type that should be used to pass the function or function object as a parameter. + | +||
The type returned by the function or function object. + | +||
The type of the first argument to the function or function object. + | +||
The type of the second argument to the function or function object. + | +
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, + +
+ ++typename Operation::argument_type +
simply writing + +
+ ++typename boost::unary_traits<Operation>::argument_type +
instead. + +
In addition to the standard result and argument typedefs, these +traits templates define two additional types. + +
This is the type of the function or function object, and can be +used in declarations such as
+ ++ ++template <class Predicate> +class unary_negate : // ... +{ + // ... + private: + typename unary_traits<Predicate>::function_type pred; +}; +
If this typedef were not provided, it would not be possible to
+declare pred in a way that would allow
+
This is a type suitable for passing the function or function object +as a parameter to another function. For example, + +
+ ++template <class Predicate> +class unary_negate : // ... +{ + public: + explicit unary_negate(typename unary_traits<Predicate>::param_type x) + : + pred(x) + {} + // ... +}; +
Function objects are passed by reference to const; function +pointers are passed by value.
+ + +This library uses these traits within all function object adapters,
+theoretically rendering
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
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
+ + diff --git a/index.html b/index.html new file mode 100644 index 0000000..7f1525b --- /dev/null +++ b/index.html @@ -0,0 +1,184 @@ + + + + + + +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
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. | +
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
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:
+
|
+
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:
+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
+ + + + diff --git a/mem_fun.html b/mem_fun.html new file mode 100644 index 0000000..9721713 --- /dev/null +++ b/mem_fun.html @@ -0,0 +1,171 @@ + + + + +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
as well as the corresponding overloaded helper functions
+
The following changes have been made to the adapters as specified +in the Standard:
+ +The standard specifies
+ ++template <class S, class T, class A> class const_mem_fun1_t + : public binary_function<T*, A, S> { +public: + explicit const_mem_fun1_t(S (T::*p)(A) const); + S operator()(const T* p, A x) const; +}; +
Note that the first argument to
+
Does this matter? Well, consider what happens when we write + +
+ ++struct Foo { void bar(int) const; }; +const Foo *cp = new Foo; +std::bind1st(std::mem_fun(&Foo::bar), cp); +
We have created a
+ ++typedef Foo* first_argument_type; +
The
This hack will not suffice with the improved binders in this library, so we have had to +provide corrected versions of the member function adapters as well. + + +
The standard defines
+ ++template <class S, class T, class A> class mem_fun1_t + : public binary_function<T*, A, S> { +public: + explicit mem_fun1_t(S (T::*p)(A)); + S operator()(T* p, A x) const; +}; +
Note that the second argument to
However, if we were to try and eliminate this inefficiency by
+instead declaring the argument as
So the way in which we want to declare the second argument for
+
The Boost
+ ++S operator()(T* p, typename call_traits<A>::param_type x) const +
we achieve the desired result - we improve efficiency without +generating references to references.
+ +The call traits template used to realise some improvements relies
+on partial specialisation, so these improvements are only available on
+compilers that support that feature. With other compilers, the
+argument passed to the member function (in the
+
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
+ + + diff --git a/negators.html b/negators.html new file mode 100644 index 0000000..51891f4 --- /dev/null +++ b/negators.html @@ -0,0 +1,132 @@ + + + + +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
As well as the corresponding helper functions
+ +However, the negators in this library improve on the standard +versions in two ways: + +
Usage is identical to the standard negators. For example,
+ ++ ++bool bad(const Foo &foo) { ... } +... +std::vector<Foo> c; +... +std::find_if(c.begin(), c.end(), boost::not1(bad)); +
The C++ Standard
+ ++template <class Predicate> + class unary_negate + : public unary_function<typename Predicate::argument_type,bool> { +public: + explicit unary_negate(const Predicate& pred); + bool operator()(const typename Predicate::argument_type& x) const; +};
Note that if the Predicate's
However, if we instead defined
So how we want to declare the argument for
+
The Boost
+ ++bool operator()(typename call_traits<typename Predicate::argument_type>::param_type x) const +
the desired result would be achieved - we would eliminate +references to references without loss of efficiency. In fact, the +actual declaration is slightly more complicated because of the use of +function object traits, but the effect remains the same.
+ +Both the function object traits and call traits used to realise
+these improvements rely on partial specialisation, these improvements
+are only available on compilers that support that feature. With other
+compilers, the negators in this library behave very much like those
+in the Standard -
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
+ + + diff --git a/ptr_fun.html b/ptr_fun.html new file mode 100644 index 0000000..f45e879 --- /dev/null +++ b/ptr_fun.html @@ -0,0 +1,135 @@ + + + + +![]() |
+ Home | +Libraries | +People | +FAQ | +More | +
The header
As well as the corresponding helper function template:
+ +However, you should not need to use the adapters in conjunction +with the adapters in this library due to our use of function object traits. You will +however need to use them if your implementation fails to work properly +with our traits classes (due to lack if partial specialisation), or if +you wish to use a function object adapter from a third party. + +
If you need to use these adapters, usage is identical to the +standard function pointer adapters. For example,
+ ++ ++bool bad(std::string foo) { ... } +... +std::vector<std::string> c; +... +std::vector<std::string>::iterator it + = std::find_if(c.begin(), c.end(), std::not1(boost::ptr_fun(bad))); +
Note however that this library contains enhanced negators that support function object traits, +so the line above could equally be written + +
+ ++std::vector<std::string>::iterator it + = std::find_if(c.begin(), c.end(), boost::not1(bad)); +
The standard defines
+
+ ++template <class Arg, class Result> +class pointer_to_unary_function : public unary_function<Arg, Result> { +public: + explicit pointer_to_unary_function(Result (* f)(Arg)); + Result operator()(Arg x) const; +}; +
Note that the argument to
However, if we were to try and eliminate this inefficiency by
+instead declaring the argument as
So the way in which we want to declare the argument for
+
The Boost
+ ++Result operator()(typename call_traits<Arg>::param_type x) const +
we achieve the desired result - we improve efficiency without +generating references to references.
+ +The call traits template used to realise this improvement relies +on partial specialisation, so this improvement is only available on +compilers that support that feature. With other compilers, the +argument passed to the function will always be passed by +reference, thus generating the possibility of references to references. + +
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
+ + +