什么情况比较好?

发布于 2024-10-07 16:51:54 字数 806 浏览 5 评论 0原文

我有一个 MyClass 列表:

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

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

results.remove_if(
    std::bind2nd(std::mem_fun_ref(&MyClass::is_old_result), lifetime));

results.remove_if(boost::bind(&MyClass::is_old_result, _1, lifetime));

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

,为什么?

PS 请不要使用 lambda 函数,也不要使用 C++0x。

I have a list of MyClass:

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

What case of removing is better (c++ design and perfomance):

results.remove_if(
    std::bind2nd(std::mem_fun_ref(&MyClass::is_old_result), lifetime));

or

results.remove_if(boost::bind(&MyClass::is_old_result, _1, lifetime));

or

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

and why?

P.S. Please, no lambda-function and no C++0x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-10-14 16:51:54

从设计上来说,使用bind的绝对是最清晰的。 (后面是显式函数对象)。为什么?简洁。

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

In terms of design, the one using bind is definitely the clearest. (followed by the explicit function object). Why? Succinct.

In terms of performance, the function object should be unbeatable (everything can be easily analysed and inlined). Depending on how the compiler optimizes, the one using bind could possibly match it (the call to is_old_result may or may not be through a pointer, depending on the compiler's analysis).

似狗非友 2024-10-14 16:51:54

如果有更合适的命名,例如“IsOldResult”或“ResultOlderThan”,我会说最终的解决方案将是最具可读性的,因为它是最适合散文的解决方案:

results.remove_if(ResultOlderThan(lifetime));

但是,我可能只会去写函子如果它所代表的算法出现在多个上下文中。对我来说,编写一个从其单行调用站点中物理删除的 5 行类似乎过于浪费。

否则, boost::bind 选项得到了我的投票,因为它和 std::bind2nd (分别是 _1 和 std::mem_fun_ref )之间的附加绒毛最少。此外,boost::bind 通常适用于更多情况,例如您不只绑定只有两个参数的函数的一个变量的情况。

With more suitable naming, such as "IsOldResult" or "ResultOlderThan", I would say that the final solution would be the most readable, as it is the one that most passes for prose:

results.remove_if(ResultOlderThan(lifetime));

However, I would probably only go and write the functor if the algorithm it represented turned up in multiple contexts. Writing a 5-line class that is physically removed from its single one-liner call site seems overly wasteful, to me.

Otherwise, the boost::bind option has my vote since it has the least additional fluff between it and std::bind2nd (_1 and std::mem_fun_ref, respectively). In addition, boost::bind works for more cases in general, such as a case where you are not binding only one variable of a function that has only two parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文