运算符()的含义?

发布于 2024-10-30 18:01:41 字数 184 浏览 1 评论 0原文

C++中operator()的含义是什么?我发现它经常用于“函子”或函数对象,例如比较器。那么这些函数是如何调用的呢?在其他情况下有用吗?我可以为这样的运算符声明多少个参数?例如,以下内容可以接受吗?

bool operator() (Foo f, Baz b, Quz q, Oik o) {...}

What is the meaning of operator() in C++? I see that is often used for "functors," or function objects such as comparators. But then how are such functions called? Is it useful in other situations? And how many parameters can I declare for such an operator? E.g., is the following acceptable?

bool operator() (Foo f, Baz b, Quz q, Oik o) {...}

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

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

发布评论

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

评论(5

一杯敬自由 2024-11-06 18:01:41

假设您有一个定义了 operator() 的函数对象类 Func。如果您有该类的实例,则只需在引用该实例的表达式后面放置括号即可:

Func myFunc;
myFunc(); // Calls the operator() member function

作为标准库中的示例,我们可以看一下 std::less,它是一个二进制函数object:

std::less<int> myLess; // Create an instance of the function object
std::cout << myLess(5, 6) << std::endl; // Is 5 less than 6?

operator() 的另一个常见用途是创建 Matrix 类。您可以定义T& Matrix::operator()(int,int) 从矩阵中检索元素,如 myMatrix(1,2)

operator() 可以采用的参数数量与任何其他函数相同。但这是实现定义的。您的实现应允许的建议参数最小数量为 256 个(在标准附录 B 中给出)。


operator() 的查找在标准 (ISO/IEC 14882:2003 §13.3.1.1.2) 中定义为:

如果函数调用语法中的主表达式 E 计算结果为“cv T”类型的类对象,则集合候选函数至少包括T的函数调用运算符。 T 的函数调用运算符是通过在 (E).operator() 上下文中普通查找名称 operator() 来获得的。

翻译:如果使用语法 expression() 进行函数调用,并且括号之前的表达式计算为类的实例,则添加对象 operator() 成员function 到可以调用的候选函数列表。

Let's say you have a function object class, Func, with operator() defined. If you have an instance of that class, you can simply place parentheses after the expression referring to that instance:

Func myFunc;
myFunc(); // Calls the operator() member function

As an example from the standard library, we can look at std::less which is a binary function object:

std::less<int> myLess; // Create an instance of the function object
std::cout << myLess(5, 6) << std::endl; // Is 5 less than 6?

Another common use for operator() is when creating a Matrix class. You may define T& Matrix::operator()(int,int) to retrieve an element from the matrix like myMatrix(1,2).

The number of parameters that operator() can take is the same as any other function. This is implementation-defined though. The recommended minimum number of arguments that your implementation should allow is 256 (given in Annex B of the standard).


operator()'s lookup is defined in the standard (ISO/IEC 14882:2003 §13.3.1.1.2) by:

If the primary-expression E in the function call syntax evaluates to a class object of type "cv T", then the set of candidate functions includes at least the function call operators of T. The function call operators of T are obtained by ordinary lookup of the name operator() in the context of (E).operator().

Translation: If you make a function call using the syntax expression() and the expression before the parentheses evaluates to an instance of a class, then add the objects operator() member function to the list of candidate functions that may be called.

几味少女 2024-11-06 18:01:41

如果您为类 C 实现 operator(),那么如果 cC 类型的对象,您可以编写c(),它调用c.operator()()。您可以为 operator() 添加任意数量的参数。

有关 operator() 何时有用的一个很好的示例,请参见此处:

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

If you implement operator() for a class C, then if c is an object of type C you can write c(), which invokes c.operator()(). You can have as many parameters to operator() as you like.

For one good example of when operator() is useful, see here:

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

城歌 2024-11-06 18:01:41

我没有看到提到的一点是,尽管在语法上它的调用看起来与常规非成员(我敢说,C 风格)函数非常相似。一个区别是,由于函子/函数对象是类/结构的实例,因此它可以具有关联的状态。

对于非成员函数,这通常意味着静态局部变量(通常不是维护状态的最佳或最有用的方法)或全局变量(通常是不可取的)。而函子/函数对象与 C++ 的封装概念很好地结合在一起,其中状态/数据与对其进行操作的函数紧密相关。

一个有启发性的例子是:

//Declaration
struct instructive_example
{
  instructive_example()
: _invocation_count(0) {};

  uint get_invocation_count() const
  { return _invocation_count; }

  void operator()()
  {
    //Do something

    //And/or operate on the state associated with this function object
    //In this case it is a simple increment of _invocation_count
    ++_invocation_count;
  }
private:
  uint _invocation_count;
};

//Instantiation & invocation:
instructive_example eg;

eg();

One point which I didn't see mentioned, was that although in syntax its invocation looks very similar to that of a regular non-member (dare I say, C-style) function. One difference is that since a functor/function-object is an instance of a class/struct, it can have an associated state.

For non-member functions this could usually mean, either static locals(often times not the best or most useful way to maintain state) or globals(often undesirable). Whereas functors/function objects tie in nicely with C++'s notions on encapsulation where in the state/data is closely tied to the functions that operate on it.

An instructive example of this is:

//Declaration
struct instructive_example
{
  instructive_example()
: _invocation_count(0) {};

  uint get_invocation_count() const
  { return _invocation_count; }

  void operator()()
  {
    //Do something

    //And/or operate on the state associated with this function object
    //In this case it is a simple increment of _invocation_count
    ++_invocation_count;
  }
private:
  uint _invocation_count;
};

//Instantiation & invocation:
instructive_example eg;

eg();
-小熊_ 2024-11-06 18:01:41

与任何重载运算符一样,operator() 是一个看起来很奇怪的函数名称。它必须是类或结构的成员函数,但除了所有函数的通常规则之外,对其参数没有任何限制。

x 的类型是类类型时,要计算表达式 x(args),C++ 将尝试调用 x.operator()(args),其中 args 可以是空字符串或任意数量的函数论据。

Like any overloaded operator, operator() is a strange-looking name of a function. It must be a member function of a class or struct, but there are no restrictions on its arguments other than the usual rules for all functions.

To evaluate the expression x(args) when the type of x is a class type, C++ will attempt to call x.operator()(args), where args can be the empty string or any number of function arguments.

走过海棠暮 2024-11-06 18:01:41

它只是一个普通函数,当您将函数调用语法应用于用户定义类型时,就会调用该函数。

因此它具有常规成员函数的所有限制和功能。它可以是一个模板,它可以接受任意数量的参数,它需要一个返回类型,它可以是 const 等。

It's just a normal function that gets called when you apply function-call syntax to a user-defined type.

So it has all the restrictions and features of a regular member function. It can be a template, it can take as many parameters as you want, it needs a return type, it can be const, etc.

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