C++ Language/Std/Stl/CallableObjects/FunctionNameAsValue

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The most simple form of a callable-object is the name of some existing function. If your software has already defined int DoAddition(int x, int y) {return x+y;}, then you might choose to define a callable-object as std::function<int(int,int)> DoMath = DoAddition;. In this case, the callable-object (DoMath) is a variable that can be manipulated like any other C++ variable. But the special thing about a callable-object is that it can be "invoked", either by a function-call-operator DoMath(11,22) or by std::invoke(DoMath, 11, 22).

Additional information about using a function name as a value (includes interactive examples)