5

<Update> As usual for me, the question was a wrong one. The actual question is: why doesn't transform_iterator use the conventional result_of<> metafunction to determine the return type, instead of accessing UnaryFunc::result_type directly. Posted an answer with a work around. </Update>

Specifically, is there a way to make a phoenix expression expose a result_type type as expected for the std::unary_function concept? boost::transform_iterator seems to expect this, and from looking at the src of it, I don't see a simple work around.

Here's some code that reproduces the problem I've been having:

#include <boost/iterator/transform_iterator.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <numeric>
#include <iostream>

using namespace boost::phoenix;
using namespace boost::phoenix::arg_names;

int main(void){
   int i[] = {4,2,5,3};

   std::cout <<
      std::accumulate(
         boost::make_transform_iterator(i,   _1*_1),
         boost::make_transform_iterator(i+4, _1*_1),
         0
      ) << std::endl;

   return 0;
}

The relavent portion of the error message from compiling this is (gcc 4.3.4, boost 1.43):

/usr/include/boost/iterator/transform_iterator.hpp:43: error: no type named ‘result_type’ in ‘struct boost::phoenix::actor<...

I have the same problem with boost::lambda (missing result_type). I thought that I had seen similar usage for make_transform_iterator and lambda in the past, now I'm wondering if I just imagined it.

Is there a provided wrapper or some other mechanism in phoenix or lambda to expose result_type?

1 Answer 1

4

It looks like this is fixed in the boost trunk (see line 51, result_of<> instead of an indirect UnaryFunc::result_type). So this shouldn't be an issue in 1.44 and above.

Here's a workaround for boost < 1.44. The transform_iterator instantiation accesses UnaryFunc::result_type only if the Reference template parameter is not provided. So one trick is to replace make_transform_iterator with a version that calls the result_of<> meta function on the UnaryFunc and use the result for the Reference template parameter.

#include <boost/iterator/transform_iterator.hpp>
#include <boost/utility.hpp>
#include <iterator>

template <class UnaryFunc, class Iterator>
boost::transform_iterator<
   UnaryFunc,
   Iterator,
   typename boost::result_of<
      UnaryFunc(typename std::iterator_traits<Iterator>::value_type)
   >::type
>
make_trans_it(Iterator it, UnaryFunc fun){
   return
      boost::transform_iterator<
         UnaryFunc,
         Iterator,
         typename boost::result_of<
            UnaryFunc(typename std::iterator_traits<Iterator>::value_type)
         >::type
      >(it, fun);
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.