无状态类函子何时可以代替c样式函数有用? [英] When are stateless class functors useful in place of a c style function?

查看:78
本文介绍了无状态类函子何时可以代替c样式函数有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上找到了一些仿函数的好例子,例如这个,并且所有令人信服的示例似乎都在定义operator()的类中使用状态.

I've found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator().

我在书中遇到了一个示例,该示例在没有状态的情况下定义了函数调用运算符,我忍不住觉得这是一个尴尬的用法,普通样式的函数指针比使用operator()这里的每一种方式-更少的代码,更少的变量(您必须实例化比较器),由于实例化,它可能会更高效,并且不会丢失含义或封装(因为它只是一个函数).

I came across an example in a book that defines the function call operator without having state, and I can't help but feel like this is an awkward usage, and that a normal style function pointer, would be better than using operator() in every way here - less code, less variables (you have to instantiate the comparators), its probably more efficient due to the instantiation, and no loss of meaning or encapsulation (since it's just one function).

我知道std::sort允许您在operator()类和函数之间进行选择,但是由于上述逻辑,我一直只使用这些函数.

I know std::sort lets you pick between operator() classes and functions, but I've always just used the functions because of the above logic.

为什么首选班级是什么原因?

What are the reasons why a class might be preferred?

下面是示例(措辞):

class Point2D {
   //.. accessors, constructors
   int x,y;
};
class HorizComp {
public:
   bool operator()(const Point2D& p, const Point2D& q) const
   { return p.getX() < q.getX(); }
};

class VertComp {
public:
   bool operator()(const Point2D& p, const Point2D& q) const
   { return p.getY() < q.getY(); }
};

template <typename E, typename C>
void printSmaller(const E& p, const E& q, const C& isLess) {
   cout << (isLess(p, q) ? p : q) << endl; // print the smaller of p and q
}
//...
// usage in some function:
Point2D p(1.2, 3.2), q(1.5, 9.2);
HorizComp horizComp;
VertComp vorizComp;
printSmaller(p, q, horizComp);
printSmaller(p, q, vorizComp);

推荐答案

典型的原因是,当您这样做时:

The typical reason is that when you do this:

bool less_than(const Point&, const Point&);
// ...
std::sort(..., &less_than);

谓词的模板参数如下:

bool(const Point&,const Point&)

由于sort函数接收一个函数指针,因此编译器更难在std::sort()内联谓词使用.发生这种情况是因为您可能还有其他功能

Since the sort function receives a function pointer, it is more difficult for the compiler to inline the predicate use inside std::sort(). This happens because you could have another function

bool greater_than(const Point&, const Point&);

具有完全相同的类型,这意味着std::sort()实例化将在两个谓词之间共享. (请记住,我说过这会使内嵌更加困难,并非没有可能).

which has the exact same type, meaning the std::sort() instatiation would be shared between the two predicates. (remember that I said that it makes inlining more difficult, not impossible).

相反,当您这样做时:

struct less_than {
    bool operator()(const Point&, const Point&) const;
};
// ...
std::sort(..., less_than());


struct greater_than {
    bool operator()(const Point&, const Point&) const;
};
// ...
std::sort(..., greater_than());

编译器会为每个谓词为std::sort()生成唯一的模板实例化,从而更容易内联谓词的定义.

The compiler generates a unique template instantiation for std::sort() for each predicate, making it easier to inline the predicate's definition.

这篇关于无状态类函子何时可以代替c样式函数有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆