Function object: Difference between revisions

Content deleted Content added
In C and C++: Add an example of a function object that gets implicitly converted to a function pointer
Line 127:
In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or [[generic programming|template]] facilities. The expressiveness of templates allows some [[functional programming]] techniques to be used, such as defining function objects in terms of other function objects (like [[function composition (computer science)|function composition]]). Much of the C++ [[Standard Template Library]] (STL) makes heavy use of template-based function objects.
 
Another way to create a function object in C++ is to define a non-explicit conversion function to a function pointer type, a function [[reference (C++)|reference]] type, or a reference to function pointer type. Assuming the conversion does not discard [[Type_qualifier|cv-qualifiers]], this allows an object of that type to be used as a function with the same [[function signature|signature]] as the type it is converted to. Modifying an earlier example to use this we obtain the following class, which can be used in an identical manner:<ref>{{cite web|url=https://en.cppreference.com/w/cpp/language/overload_resolution#Call to a class object|title=Overload resolution§Call to a class object|website=cppreference.com}}</ref>
 
<syntaxhighlight lang="cpp">