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
7
votes
1 answer
When using templates to support functor as arguments, what qualifier should I use?
Consider this code:
template
void foo1(F f) { f(); }
template
void foo2(F const& f) { f(); }
template
void foo3(F&& f) { f(); }
Which version of foo should I use? foo1 is what I see most "in the wild" but I fear that it…

Artificial Mind
- 945
- 6
- 19
7
votes
1 answer
Is There a Indirection Functor?
I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist.
So given the code:
const auto vals = { 0, 1, 2, 3 };
vector…

Jonathan Mee
- 37,899
- 23
- 129
- 288
6
votes
4 answers
Using std::tm as Key in std::map
I'd like to use std::tm () as the key for an std::map-container.
But when I try to compile it, I get a lot(10) of errors.
For example:
1.
error C2784: 'bool std::operator
<(const
std::basic_string<_Elem,_Traits,_Alloc>
&,const _Elem *)' :…

Juarrow
- 2,232
- 5
- 42
- 61
6
votes
1 answer
Dart(/Flutter): Create function in initializer list
I am implementing a class with multiple constructors, which is internally build around a IndexedWidgetBuilder (a function object)
typedef IndexedWidgetBuilder = Widget Function(BuildContext context, int index);
Now, one of the constructors, call it…

manthano
- 213
- 3
- 7
6
votes
2 answers
Using STL algorithms with shared_ptr, function objects
I have a set of shared_ptr, and I'd like to use remove_copy_if with a custom function object for the predicate. I didn't know the "best" way to do it. Right now, I've got this working:
class CellInCol : public std::unary_function

Matt
- 5,478
- 9
- 56
- 95
6
votes
1 answer
Why can I pass a value-accepting callable to a reference-accepting std::function?
When I declare a variable function, the compiler still lets me assign a lambda that accepts a value:
function handler;
handler = [](Foo f){};
(cfr. also http://cpp.sh/5dsp)
So when the handler is called, a copy…

xtofl
- 40,723
- 12
- 105
- 192
6
votes
3 answers
Is there an idiomatic way to create a collection of delegates in C++?
I want to store functions with similar signature in a collection to do something like this:
f(vector& orders, vector& functions) {
foreach(process_orders in functions) process_orders(orders);
}
I thought of function…

Anton Daneyko
- 6,528
- 5
- 31
- 59
6
votes
2 answers
How I can pass callable object to function as parameter
In c++ standard library almost all algo function takes a callable object as a argument. Now I want to try this thing with my program. I opened the the headers for function like find_if or search_n() but could not understand much about how these…

deeiip
- 3,319
- 2
- 22
- 33
6
votes
0 answers
Function Objects security risks
I was reading an article on C++11 and the author mentioned that one of the advantages of using lambdas is to avoid the tedium and security risks of function objects.
What are some security risks of FOs?
Source:The Biggest Changes in C++11 and Why…

Justin
- 447
- 4
- 10
- 33
5
votes
2 answers
How to execute unary function objects of different parameter type in sequence?
I'm designing a mechanism that will execute a set of unary function objects in sequence. These function objects are assigned during runtime, and the problem is: the parameter type of these function objects are different.
What I want to do is…

RichardLiu
- 1,902
- 1
- 19
- 18
5
votes
4 answers
Using an std::function for wrapping a function object
Can someone help me to understand why the following code causes an error?
class A
{
public:
float& operator()()
{
return _f;
}
private:
float _f = 1;
} a;
auto& foo()
{
std::function func = a;
return…

abraham_hilbert
- 2,221
- 1
- 13
- 30
4
votes
2 answers
Is there a way to call std::equal_range with a std::function instead of a structure?
In the below example a struct Comp is created. Is there a way to make it a std::function or write the comparator itself while calling std::equal_range? Can I get rid of the struct?
struct Comp
{
bool operator() ( const S& s, int i ) const {…

abhijit jagdale
- 609
- 2
- 8
- 21
4
votes
2 answers
function objects for class methods
I know how to make a function an input argument when the function isn't a method, but I can't figure out how to do it when the function is a method.
Here's what I tried:
#include
class A
{
void funct_1(int a, void (A::*f)(int))
…

Elliott
- 2,603
- 2
- 18
- 35
4
votes
4 answers
C++ Passing function objects as lvalues and/or rvalues
I have a class that should filter its contents according to a user-supplied predicate. The interface I am given prescribes taking a reference to the predicate:
class Test {
vector data;
public:
template
void…

macleginn
- 321
- 2
- 10
4
votes
0 answers
Copy constructor is called many times when constructing a thread by function object
I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is useless.
Code Snippet:
#include
#include…

Dongxu Wang
- 41
- 5