In object-oriented languages, function object (also known as functor) is a feature that allows objects to be used like if they were ordinary functions.
Questions tagged [function-object]
168 questions
12
votes
2 answers
How do function objects affect overload resolution?
Are function objects treated differently from regular functions during overload resolution? If so, how?
I have run into the following case where replacing a function with an equivalently-callable function object changed the meaning of the…

HighCommander4
- 50,428
- 24
- 122
- 194
11
votes
4 answers
STL Functional -- Why?
In C++ Standard Template Library, there's a 'functional' part, in which many classes have overloaded their () operator.
Does it bring any convenience to use functions as objects in C++?
Why can't we just use function pointer instead? Any examples?

Determinant
- 3,886
- 7
- 31
- 47
11
votes
3 answers
Are function pointers function objects in C++?
The C++ standard defines function objects as:
A function object type is an object type that can be the type of the
postfix-expression in a function call. (link)
First I was thinking that function objects were functors, but then I realized that…

Vincent
- 57,703
- 61
- 205
- 388
11
votes
6 answers
When do you use function objects in C++?
I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?

jasonline
- 8,646
- 19
- 59
- 80
10
votes
6 answers
function objects versus function pointers
I have two questions related to function objects and function pointers,
Question : 1
When I read the different uses sort algorithm of STL, I see that the third parameter can be a function objects, below is an example
class State {
public: …

kumar_m_kiran
- 3,982
- 4
- 47
- 72
9
votes
4 answers
C++ equivalent of C#'s Func
The following code computes the average of a particular property of T in the items collection:
public double Average(IList items, Func selector)
{
double average = 0.0;
for (int i = 0; i < items.Count; i++)
{
…

aligray
- 2,812
- 4
- 26
- 35
9
votes
3 answers
How to document a function object with doxygen?
How should I document a function object (AKA functor) with doxygen? It feels misleading to just document it as a regular class. I find it much better to think of a function object as a function with a closure than a callable class.
Is there a way…

deft_code
- 57,255
- 29
- 141
- 224
9
votes
1 answer
Functors: templated struct vs templated operator()
The usual pattern for standard library function objects is to have a templated struct with a non-template operator(). For example, std::less looks something like so:
template
struct less
{
bool operator()(const T& lhs, const T& rhs)…

Tristan Brindle
- 16,281
- 4
- 39
- 82
9
votes
2 answers
Const and non-const functors
This seems like something that ought to be frequently asked and answered, but my search-fu has failed me.
I'm writing a function which I want to accept a generic callable object of some kind (including bare function, hand-rolled functor object,…

Miral
- 12,637
- 4
- 53
- 93
8
votes
3 answers
What's the meaning of &T::operator() in function traits?
// Fallback, anything with an operator()
template
struct function_traits : public function_traits > {
};
What does T::operator() mean here?
This is code from pytorch.

ben19900305
- 83
- 3
8
votes
2 answers
Detecting function object (functor) and lambda traits
How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)?
Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open…

metal
- 6,202
- 1
- 34
- 49
8
votes
5 answers
how to pass a class method as argument to another method of the class in perl 6
I have a script like the below. Intent is to have different filter methods to filter a list.
Here is the code.
2
3 class list_filter {
4 has @.my_list = (1..20);
5
6 method filter($l) { return True; }
7
8 # filter method
9 …

BhaskarS
- 305
- 1
- 6
8
votes
2 answers
What is the correct argument type for a function-object?
I have a templated function that receives function-objects. Sometimes the function-objects are stateless structs, but sometimes they are large statefull objects. The state of the function-object is not changed in this function, only examined. I'm…

Mankka
- 521
- 3
- 12
8
votes
6 answers
Recursively call a function object
How do I call a function object from within itself? Seems I cannot use this. Example:
class factorial {
public:
int operator()(int n) {
if (n == 0)
return 1;
return n * ??(n-1);
}
};
What do I place at ???

r.v
- 4,697
- 6
- 35
- 57
7
votes
3 answers
Why can C++ functors be preferable to objects with named methods?
I recently have got excited by functors and been using them all over the place. Then the situation arose where I needed my functor to perform two different operations and I thought about adding another method to my functor (not overloading the ()…

zenna
- 9,006
- 12
- 73
- 101