FUNCTORS(3)
π NAME π
Function Objects(3) β Function Objects β from the Library Functions Manual β¨ aka the functor fam page, bestie π«Ά
π Classes π
struct std::binary_function< _Arg1, _Arg2, _Result >struct std::unary_function< _Arg, _Result >
π§ Modules π§
- Binder Classes
- Arithmetic Classes
- Comparison Classes
- Boolean Operations Classes
- Negators
- Adaptors for pointers to functions
- Adaptors for pointers to members
β¨ Detailed Description β¨
Okay so function objects, aka functors, are literally objects with an operator() defined and accessible. No cap. They can be passed as arguments to algorithm templates and used in place of a function pointer. Not only is the resulting expressiveness of the library increased, but the generated code can be more efficient than what you might write by hand. When we refer to 'functors,' then, generally we include function pointers in the description as well. π
Often, functors are only created as temporaries passed to algorithm calls, rather than being created as named variables. Itβs giving βtemporary main characterβ energy. π¦
Two examples taken from the standard itself follow. To perform a by-element addition of two vectors a and b containing double, and put the result in a, use
transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
To negate every element in a, use
transform(a.begin(), a.end(), a.begin(), negate<double>());
The addition and negation functions will be inlined directly. Slay, compiler. π₯
The standard functors are derived from structs named unary_function and binary_function. These two classes contain nothing but typedefs, to aid in generic (template) programming. If you write your own functors, you might consider doing the same. iykyk. π
πΈ Author πΈ
Generated automatically by Doxygen for libstdc++ from the source code.
Thatβs the whole vibe check, bestie π β¨ This page is from libstdc++, dated 21 Apr 2009 β Function Objects(3) forever πππ¦
π¦ where this page comes from
- nixpkgs unstable: std-man-pages 4.4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms