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

查看:109
本文介绍了什么是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.

如果我通过了一个函数指针,则编译器无法t会立即看到它指向哪个函数,因此,除非执行一些相当复杂的全局优化,否则必须在运行时取消引用指针,然后进行调用。

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天全站免登陆