|
Boost Users : |
From: Ian McCulloch (ianmcc_at_[hidden])
Date: 2005-03-04 13:27:42
Matthias Kaeppler wrote:
> Hello,
>
> I have written an adaptor for functions and function objects which
> allows to invoke operations on the pointees when working on a collection
> of pointers. For example, it allows to sort a list of pointers by a
> predicate applying to the pointees.
> To make it work with normal functions and functors, I utilized
> boost::unary_traits and boost::binary_traits.
> This is the class template for the unary version:
>
> template< typename Operation >
> class indirecter_unary
> : public std::unary_function< typename
> boost::unary_traits<Operation>::argument_type,
Is this correct? You are advertising that your function has an
argument_type that is the same as Operation::argument_type. Don't you want
your argument_type to be a pointer? In any event, since you need to
redefine argument_type and result_type yourself, there is no point
inheriting from std::unary_function.
> typename boost::unary_traits<Operation>::result_type >
> {
> typedef typename boost::unary_traits<Operation>::argument_type
> arg_type;
I think, the argument_type you are looking for is
typedef typename boost::remove_reference<arg_type>::type* argument_type;
> typedef typename boost::unary_traits<Operation>::param_type
> param_type;
IIUC, param_type is supposed to be a type used to pass this function object
as a parameter. ie, it should be something like indirecter_unary const&,
NOT the param_type of Operation.
> typedef typename boost::unary_traits<Operation>::result_type
> result_type;
> typedef typename boost::unary_traits<Operation>::function_type
> function_type;
> function_type op_;
> public:
> explicit indirecter_unary( param_type op ): op_(op) {}
> result_type operator() (arg_type *arg) const {
^^^^^^^^^^
should be:
result_type operator()(argument_type arg) const {
> return op_(*arg);
> }
> };
>
> Now, the problem arises when I pass a function which takes references.
> Like this:
>
> bool pred( int& a )
> {
> //...
> }
>
> indirecter_unary< bool ()(int&) > fctor(pred);
>
> ext::indirecter_unary<bool ()(int&)>':
> code/FileBrowser.cpp:90: instantiated from here
> code/ext/indirect.hpp:80: error: forming pointer to reference type `int&'
>
> Line 80 is the line where operator() is defined. The error message
> indicates that argument_type is obviously not correct in this context.
> I thought the boost traits would take care of the right argument types?
> Am I missing something?
>
Operation::argument_type is whatever the argument type of the operator is.
It is up to you to transform that into whatever argument type you want for
your adaptor! In this case, you want to remove a reference (if there is
one), and add a pointer.
HTH,
Ian
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net