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
3
votes
1 answer
Converting a Functor to an Object of Type function
Say I have this function:
template
void foo(function op, const T& lhs, const T& rhs) {
cout << op(lhs, rhs) << endl;
}
This is legal code:
function op = plus();
foo(op, 13,…

Jonathan Mee
- 37,899
- 23
- 129
- 288
3
votes
4 answers
templates and function objects - c++
i have a problem with this class.
the goal is to make the main function work properly. we were supposed to implement the "And" function object so that the code will work. i can't find what is the problem with our solution. (the solution start and…

bks
- 1,886
- 1
- 24
- 43
3
votes
2 answers
Does returning a bound local function object cause undefined behaviour?
I am giving an example below. The program compiles and runs fine, but I am wondering whether it is theoretically undefined behaviour according to the C++11 standard; can I return the result of binding a (temporary) local function object?
Example…

Jonathan H
- 7,591
- 5
- 47
- 80
2
votes
2 answers
Can I use a function object without instantiation?
Having the following code:
template struct print {
OutStream &operator()(T const &toPrint, OutStream &outStream = std::cout) const {
outStream << toPrint;
return outStream;
}
};
This call…

nickolay
- 3,643
- 3
- 32
- 40
2
votes
1 answer
unary_function and binary_function are deprecated in C++ 11, and removed in C++ 17. What should we use instead?
I have been reading Effective STL by Meyers.
I came across some sections which mention function adapter objects, such as not1, bind1st, bind2nd. There are apparently a range of such function adapter objects, however I have never encountered these…

FreelanceConsultant
- 13,167
- 27
- 115
- 225
2
votes
2 answers
Binding the parameters before setting the function pointer?
I would like to try something out and unify some boilerplate code in one of our dynamic library API wrappers.
Essentially, I would like to do the following:
typedef bool (*MyFPtrT)(long id, std::string const& name);
typedef boost::function

Martin Ba
- 37,187
- 33
- 183
- 337
2
votes
2 answers
Functional Object in Polymorphism
I want to implement Functional Object in Polymorphism as follows:
#include
#include
using namespace std;
struct Compare {
virtual bool operator() (int, int) const = 0;
};
struct Less : public Compare {
bool operator()…

coco cheng
- 21
- 2
2
votes
1 answer
Proper way of returning a functor in C++
Consider we have an add function declared like this:
int add(const int a, const int b);
If we were to return this add function from foo...
std::function foo()
{
return add;
}
std::function…

Yiğit
- 55
- 6
2
votes
1 answer
Function object to pass std::get around as an argument to functions
My intention is to write a getter for the Nth element of whatever std::get can take as argument once it's instantiated with a specific N.
In other words, std::get cannot be passed around because it's not a function object, but a function…

Enlico
- 23,259
- 6
- 48
- 102
2
votes
1 answer
Function ignoring its argument(s)
This is mostly a curiosity question.
I saw code like this (I'm not interested in what it does)
constexpr auto xxx = boost::hana::overload(
[](SomeType& x){ /* x is used and something is returned */ },
[](SomeOtherType1&){ /* no-op in this…

Enlico
- 23,259
- 6
- 48
- 102
2
votes
3 answers
How is factorial computed?
say there is a function to calculate factorial(n)
Does factorial(7) creates 7 function object for each of n from 1 to 7
and use those values when ever necessary (for factorial(8) as like factorial(7)*8)

yesraaj
- 46,370
- 69
- 194
- 251
2
votes
3 answers
returning a user defined function name when using a decorator with a callable object
Consider the following code fragment.
def print_timing(func):
import time
def wrapper(*args, **kwargs):
t1 = time.time()
res = func(*args, **kwargs)
t2 = time.time()
print '%s took %0.3f s ~ %0.0f min and…

Faheem Mitha
- 6,096
- 7
- 48
- 83
2
votes
0 answers
C++ primer 5. ed. A lambda closure in place of a function pointer not allowed?
In C++ primer chapter 14. Function objects:
// ordinary function
int add(int i, int j) { return i + j; }
// lambda, which generates an unnamed function-object class
auto mod = [](int i, int j) { return i % j; };
// function-object class
struct div…

Maestro
- 2,512
- 9
- 24
2
votes
4 answers
Is function object created by `new` constructor treated as mutable object in javascript?
From study, I understood that in javascript, mutable objects() are treated by call-by-reference, and immutable objects are treated by call-by-value calling convention.
Let's say I use this kind of data,
var Node = function(data) {
this.data =…

rokrokss
- 137
- 2
- 11
2
votes
5 answers
How can I use std::generate/generate_n with a polymorphic function object?
I'm new to std::generate and have attempted to structure a program which uses it to initialize vectors. However it's behaving differently to my expectations.
I have an abstract base class:
template
class RandomAllele {
public:
…

Rhys van der Waerden
- 3,526
- 2
- 27
- 32