boost::phoenix::sort error

275 Views Asked by At

I am trying to sort a vector below using boost::phoenix library. The class Foo has a member function 'int getvalue()'. The purpose is to sort the vector using the value returned by 'getvalue()'. But something is missing. I get compiler error as '::second is not a class or namespace'

      std::vector<std::pair<int, Foo> > fooVec;

      boost::phoenix::sort ( boost::phoenix::bind( &std::pair<int, Foo>::second::getvalue(), boost::phoenix::arg_names::arg1) (*fooVec.begin() ), std::less<int>() );

Can anybody please explain this. What changes do I need to make this work?

Thanks.

PS: I know I could have used function object/lambda or soemthing similar but I wanted to try out boost::phoenix.

2

There are 2 best solutions below

2
On BEST ANSWER

As commented, I don't think that your way of creating a phoenix actor from your vector can be used to sort it, but I never used the algorithms from phoenix so I not sure about it. You can of course use sort and create a functor with phoenix to sort it.
So I would suggest to use phoenix this way.

boost::phoenix::sort(boost::phoenix::placeholders::arg1, boost::phoenix::placeholders::arg2)(fooVec, 
        boost::phoenix::bind( &Foo::getvalue, boost::phoenix::bind( &std::pair<int, Foo>::second, boost::phoenix::placeholders::arg1)) < boost::phoenix::bind( &Foo::getvalue, boost::phoenix::bind( &std::pair<int, Foo>::second, boost::phoenix::placeholders::arg2))
        );
5
On

It's just what the error message says. std::pair<int, Foo>::second is a data member, not a class or namespace, so you cannot use operator :: on it.

Instead of second, you can use second_type, which is a typedef for the type of the second element in the pair:

boost::phoenix::sort ( boost::phoenix::bind( &std::pair<int, Foo>::second_type::getvalue, boost::phoenix::arg_names::arg1) (*fooVec.begin() ), std::less<int>() );