什么是 C++ 函子及其用途? [英] What are C++ functors and their uses?

查看:27
本文介绍了什么是 C++ 函子及其用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直听到很多关于 C++ 中的函子的消息.有人能给我一个关于它们是什么以及它们在什么情况下有用的概述吗?

I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?

推荐答案

仿函数几乎只是一个定义 operator() 的类.这使您可以创建看起来像"函数的对象:

A functor is pretty much just a class which defines the operator(). That lets you create objects which "look like" a function:

// this is a functor
struct add_x {
  add_x(int val) : x(val) {}  // Constructor
  int operator()(int y) const { return x + y; }

private:
  int x;
};

// Now you can use it like this:
add_x add42(42); // create an instance of the functor class
int i = add42(8); // and "call" it
assert(i == 50); // and it added 42 to its argument

std::vector<int> in; // assume this contains a bunch of values)
std::vector<int> out(in.size());
// Pass a functor to std::transform, which calls the functor on every element 
// in the input sequence, and stores the result to the output sequence
std::transform(in.begin(), in.end(), out.begin(), add_x(1)); 
assert(out[i] == in[i] + 1); // for all i

函子有一些不错的地方.一是与常规函数不同,它们可以包含状态.上面的例子创建了一个函数,它把你给的任何东西都加 42.但是值 42 不是硬编码的,它在我们创建函子实例时被指定为构造函数参数.我可以创建另一个加法器,它添加了 27,只需调用具有不同值的构造函数即可.这使它们可以很好地自定义.

There are a couple of nice things about functors. One is that unlike regular functions, they can contain state. The above example creates a function which adds 42 to whatever you give it. But that value 42 is not hardcoded, it was specified as a constructor argument when we created our functor instance. I could create another adder, which added 27, just by calling the constructor with a different value. This makes them nicely customizable.

如最后几行所示,您经常将函子作为参数传递给其他函数,例如 std::transform 或其他标准库算法.您可以对常规函数指针执行相同操作,但正如我上面所说,函子可以自定义",因为它们包含状态,从而使它们更加灵活(如果我想使用函数指针,我必须编写一个函数它在它的参数中正好加了 1.函子是通用的,并添加了你初始化它的任何东西),而且它们也可能更有效.在上面的例子中,编译器确切地知道应该调用哪个函数 std::transform.它应该调用 add_x::operator().这意味着它可以内联该函数调用.这使得它的效率就像我在向量的每个值上手动调用函数一样.

As the last lines show, you often pass functors as arguments to other functions such as std::transform or the other standard library algorithms. You could do the same with a regular function pointer except, as I said above, functors can be "customized" because they contain state, making them more flexible (If I wanted to use a function pointer, I'd have to write a function which added exactly 1 to its argument. The functor is general, and adds whatever you initialized it with), and they are also potentially more efficient. In the above example, the compiler knows exactly which function std::transform should call. It should call add_x::operator(). That means it can inline that function call. And that makes it just as efficient as if I had manually called the function on each value of the vector.

如果我传递了一个函数指针,编译器不能立即看到它指向哪个函数,所以除非它执行一些相当复杂的全局优化,否则它必须在运行时取消引用该指针,然后使打电话.

If I had passed a function pointer instead, the compiler couldn't immediately see which function it points to, so unless it performs some fairly complex global optimizations, it'd have to dereference the pointer at runtime, and then make the call.

这篇关于什么是 C++ 函子及其用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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