Search code examples
c++booststlboost-bindboost-phoenix

boost::phoenix::sort error


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.


Solution

  • 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))
            );