liveBook · Manning

Apply operator

As explained in 100 C++ Mistakes and How to Avoid Them

The apply operator is the C++ call operator that defines how an object is invoked with function-call syntax.

The apply operator, written operator(), defines how an object responds to function-call syntax. A class that implements it becomes a functor, so an instance can be called like a function. section 4.6

Maximum as a callable object

Maximum<int> f; lets the program call f(v). The operator() implementation scans the vector and returns its largest value. This named functor communicates intent, but a lambda can define the same behavior directly at the call site, avoiding a separate type and instantiation. section 4.6

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains operator() through a complete maximum-value implementation and invocation, then stops at a brief lambda comparison.
Examples
Uses a Maximum structure, a vector of integers, f(v) invocation, std::accumulate, and an equivalent lambda.
What you can do after
Build it from scratch by defining operator() and invoking the resulting object with function-call syntax.
How it connects
Connects functors, function objects, lambdas, functional programming, and standalone functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.