什么案例更好?

Ale*_*tov 7 c++ performance stl language-design functor

我有一个清单MyClass:

struct MyClass {
     bool is_old_result(int lifetime);
};
std::list<MyClass> results;
int lifetime = 50; // or something else
Run Code Online (Sandbox Code Playgroud)

什么情况下删除更好(c ++设计和性能):

results.remove_if(
    std::bind2nd(std::mem_fun_ref(&MyClass::is_old_result), lifetime));
Run Code Online (Sandbox Code Playgroud)

要么

results.remove_if(boost::bind(&MyClass::is_old_result, _1, lifetime));
Run Code Online (Sandbox Code Playgroud)

要么

struct RemoveFunctor {
   RemoveFunctor (int lifetime) : lifetime(lifetime) {}
   bool operator()(const MyClass & m) { return m.is_old_result(lifetime); }
private:
   int lifetime;
};
results.remove_if(RemoveFunctor(lifetime));
Run Code Online (Sandbox Code Playgroud)

为什么?

PS请,没有lambda函数,没有C++ 0x.

lij*_*jie 12

在设计方面,使用bind的绝对是最清晰的.(后跟显式函数对象).为什么?简洁.

在性能方面,功能对象应该是无与伦比的(一切都可以轻松分析和内联).根据编译器的优化方式,使用者bind可能会匹配它(is_old_result根据编译器的分析,调用可能会或可能不会通过指针).

  • 我也同意,并且我将补充说,除非/直到您分析并确定此声明是性能瓶颈,否则性能应该是次要问题. (3认同)