I have a Visual Studio 2008 C++ application where I would like to replace a unary functor with a boost::phoenix lambda expression.
In my case, I have list of objects with containing a string. I want to remove all objects with a string that does not match the specified one. So, I use an algorithm like this:
struct Foo
{
std::string my_type;
};
struct NotMatchType
{
NotMatchType( const std::string& t ) : t_( t ) { };
bool operator()( const Foo& f ) const
{
return f.my_type.compare( t_ ) != 0;
};
std::string t_;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector< Foo > list_of_foo;
/*populate with objects*/
std::string some_type = "some type";
list_of_foo.erase(
std::remove_if( list_of_foo.begin(),
list_of_foo.end(),
NotMatchType( some_type ) ),
list_of_foo.end() );
return 0;
}
This works fine. But, I’d like to clean up my code a bit and get rid of the NotMatchType functor and replace it with a simple lambda expression like this:
using boost::phoenix::arg_names::arg1;
list_of_foo.erase(
std::remove_if( list_of_foo.begin(),
list_of_foo.end(),
arg1.my_type.compare( some_type ) != 0 ),
list_of_foo.end() );
obviously, this doesn’t work.
I have also tried: ( arg1->*&Foo::my_type ).compare( some_type ) != 0
What do I need to do to make the boost:phoenix:actor look like a Foo object?
Given two strings
lhsandrhs, thenlhs == rhsis specified to be semantically equivalent tolhs.compare(rhs) == 0. In other words, what your functor is doing is equivalent to doingf.my_type != t_.With that in mind, you can express what you want with Phoenix as:
For the record, you were calling a member
compareon a Phoenix actor. Since that member belongs tostd::stringthough, that’s not what you want. I can get the following to work:where that last line is the final functor. But that’s not good because there is no reliable way to get the address of an overloaded member of a Standard type. In other words the second line in the above is not guaranteed to compile.
For future reference I prefer lambdas when calling an overloaded member: