Delegate in C++11

后端 未结 5 959
悲&欢浪女
悲&欢浪女 2021-02-14 00:09

Does C++11 provide delegates?

If not, what is the best (most efficient) way to do something similar in C++? Boost.Signals? FastDelegate? Something else?

5条回答
  •  没有蜡笔的小新
    2021-02-14 00:17

    You don't need c++0x. in you have bind1st bind2nd mem_fun and mem_fun_ref. You also have Boost.Bind which generalizes all of the above functions (IIRC).

    Going from memory ...

    vector foo = makeVector();
    vector foop = makeVectorP();
    vector bar1,bar2,bar3,bar4;
    transform( foo.begin(), foo.end(), back_inserter( bar1 ), mem_fun_ref(&Foo::getBar) );
    transform( foop.begin(), foop.end(), back_inserter( bar2 ), mem_fun(&Foo::getBar) );
    transform( foo.begin(), foo.end(), back_inserter( bar3 ), bind1st(&bar_from_foo) );
    transform( foo.begin(), foo.end(), back_inserter( bar4 ), boost::bind(&bar_from_foo, _1) );
    

提交回复
热议问题