开发者

replacing a unary functor with a boost::phoenix actor

开发者 https://www.devze.com 2023-03-31 18:57 出处:网络
I have a Visual Studio 2008 C++ application where I would like to replace a unary functor with a boost::phoenix lambda expression.

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?


Using std::string::compare() directly from Phoenix is pretty ugly, since it's overloaded and we need to take its address:

phx::bind(
    static_cast<int (std::string::*)(std::string const&) const>(
        &std::string::compare
    ),
    phx::cref(some_type),
    phx::bind(&Foo::my_type, arg1)
) != 0

However, if we follow Luc's hint and simply compare object equality, it becomes more manageable:

phx::cref(some_type) != phx::bind(&Foo::my_type, arg1)


Given two strings lhs and rhs, then lhs == rhs is specified to be semantically equivalent to lhs.compare(rhs) == 0. In other words, what your functor is doing is equivalent to doing f.my_type != t_.

With that in mind, you can express what you want with Phoenix as:

bind(&Foo::my_type, arg1) =! ref(some_type)

For the record, you were calling a member compare on a Phoenix actor. Since that member belongs to std::string though, that's not what you want. I can get the following to work:

typedef int (std::string::*compare_type)(std::string const&) const;
compare_type compare = &std::string::compare;
bind(compare, bind(&Foo::my_type, arg1), "") != 0;

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:

auto compare = [](std::string const& lhs, std::string const& rhs)
{ return lhs.compare(rhs); };
// bind that functor and use it as a Phoenix actor etc
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号