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
4
votes
1 answer
How to pass function by reference?
I have a C++ program that has free standing functions.
Since most of the team has little experience with or knowledge of object oriented design and programming, I need to refrain from function objects.
I want to pass a function to another…

Thomas Matthews
- 56,849
- 17
- 98
- 154
4
votes
2 answers
Why doesn't N3421 provide the noexcept qualifier?
In N3421 - Making Operator Functors greater<>, the new specialization for the std function objects is:
template <> struct plus {
template auto operator()(T&& t, U&& u) const
-> decltype(std::forward(t) +…

gnzlbg
- 7,135
- 5
- 53
- 106
4
votes
2 answers
How to properly define function objects in c++?
I am getting a very strange error on a very simple code that I couldn't fix.
I have defined the following function object:
template class L2Norm {
public:
double operator()(const Point& p) {
/*…

MikeL
- 2,369
- 2
- 24
- 38
4
votes
6 answers
Declaring function objects for comparison?
I have seen other people questions but found none that applied to what I'm trying to achieve here.
I'm trying to sort Entities via my EntityManager class using std::sort and a std::vector
/*Entity.h*/
class Entity
{
public:
float…

Goles
- 11,599
- 22
- 79
- 140
4
votes
1 answer
Why do C++ function objects need reference type member variables?
This is a newbie C++ question. I was reading the "Function object" article in Wikipedia. The article has an example in C++ similar to the following:
struct printClass {
int &count;
printClass(int &n) : count(n) {}
void operator()(int &i)…

codefx
- 9,872
- 16
- 53
- 81
3
votes
4 answers
pointer to function object in C++
I wanted to pass a function object to a class, and the class will use the function object to do some job inside the class.
But the problem is that, I don't what the function object will be passed in. So I figured that, define a void * pointer in the…

Alcott
- 17,905
- 32
- 116
- 173
3
votes
1 answer
How to return a function type dependent on a template argument?
I would like to return a std::function whose type is dependent on the type of one template argument of my function template.
// Return a function object whose type is directly dependent on F
template
auto…

Martin Ba
- 37,187
- 33
- 183
- 337
3
votes
3 answers
Most terse and reusable way of wrapping template or overloaded functions in function objects
Scenario 1: a template function pred
template
bool pred(T t) { /* return a bool based on t */ }
Scenario 2: a set of functions overloaded on the same name pred
bool pred(A t) { /* return a bool based on t */ }
bool pred(B t) { /* return…

Enlico
- 23,259
- 6
- 48
- 102
3
votes
3 answers
C++ primer 5 ed. : using sort on a vector of pointers is it undefined?
In C++ primer 5 ed. page 172:
"We cannot use the relational operators on pointers to two unrelated objects:
int i = 0, sz = 42;
int *p = &i, *e = &sz;// undefined: p and e are unrelated; comparison is meaningless!
while (p < e) ".
But if I run…

Maestro
- 2,512
- 9
- 24
3
votes
2 answers
Forwarding vs not Forwarding a function passed to a wrapper
My question is what is the advantage of perfect forwarding a function, which is passed to our wrapper.
template
auto time_function(T&& func, U&& ...args)
{
std::cout<<"timing void function"<

Sridhar Thiagarajan
- 580
- 1
- 7
- 20
3
votes
1 answer
C++ Functor Syntax
I have a question regarding the syntax of how to call a functor in C++. In the code below, why do the first 2 examples work whilst the 3rd and 4th attempt do not?
Also I can call ob (100); and it will output 300, but why can't I call Class(100) and…

BYS2
- 5,199
- 5
- 24
- 32
3
votes
0 answers
How can std::function accept all function objects with the same function call operator signature?
I am curious as to how std::function can accept objects of different types as long as they have the same function call operator signature. For instance, these two structs are clearly unrelated, and yet, the same std::function object can be assigned…

GamefanA
- 1,555
- 2
- 16
- 23
3
votes
5 answers
What happens to variables/objects in inner classes of function objects?
I have a function multi2 which returns inner class Inner as an Object.
What happens to a - where is it saved and how can I access it?
public class C {
private static Object multi2(final int a) {
class Inner {
public int…

Dennis Vash
- 50,196
- 9
- 100
- 118
3
votes
3 answers
Why do I need pass a comparator to construct a priority_queue when it is a lambda, but not when it is std::greater?
I am reading a code sample from cppreference:
#include
#include
#include
#include
template void print_queue(T& q) {
while(!q.empty()) {
std::cout << q.top() << " ";
q.pop();
…

Sean
- 2,649
- 3
- 21
- 27
3
votes
1 answer
accessing function attributes inside decorators
Is it possible to access function attributes inside a decorator?
Consider below piece of code.
def deco(a):
def wrap():
print(a.status)
a()
print(a.status)
return wrap
@deco
def fun1():
…

Sudhan Nadar
- 77
- 9