Questions tagged [function-object]

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.

168 questions
2
votes
1 answer

Assign a method operator() to an object

I'm having this problem in a real-world Project. How to convert an "object" into a function in JavaScript? I need at least as Steve said "to assign a method operator() to an object". Re-assign is not an option, because the object is too complex and…
Gus
  • 25,839
  • 2
  • 51
  • 76
2
votes
1 answer

std::set - like function object support in my container

I have implemented my own container: template class MyContainer { // body where in some point 2 elements of collection are compared (-1, 0 and 1 possible comparison results) }; What I want to do is add support of function objects,…
Przemysław Kalita
  • 1,977
  • 3
  • 18
  • 28
2
votes
1 answer

How to accumulate results from a member function of a Element in a Container using only Element member functions and STL?

I have a Container of Elements, and each Element has its size() member function. I have managed to accumulate the total Container Elements size by writing a binary operation add_size: #include #include #include…
tmaric
  • 5,347
  • 4
  • 42
  • 75
2
votes
1 answer

C++ function objects cannot create sum using std::for_each (VS2012)

I am having trouble using function-objects in Visual Studio 2012. I created a simple std::vector, added the ints 0-9 and wanted to create the sum of it using a function object. My class definition (inline): template class Sum { private: …
bash.d
  • 13,029
  • 3
  • 29
  • 42
2
votes
2 answers

Passing inner function of a struct (or class) as a functor

How should I pass a function inside an struct as a functor? I assumed this should work fine, but it didn't: #include using namespace std; struct s { int a[10]; bool cmp(int i, int j) { // return something } …
semekh
  • 3,867
  • 2
  • 24
  • 34
2
votes
2 answers

get state in a function object casted to a std::function

I want to retrieve state from a function object. But the function object has been casted to a function<> template. How can I do it? I mean: function object: class Counter { private: int counter_; public: Counter():counter_(0) {cout <<…
jamarier
  • 23
  • 2
1
vote
2 answers

overloading operator ()

I have this declaration struct Z { void operator ()( int a ) { cout << "operator()() " << a << endl; } }; Z oz, *zp = &oz; oz(1); //ok (*zp)(2); //ok zp(3); //"error: 'zp' cannot be used as a function" Is there a way to modify…
Ulterior
  • 2,786
  • 3
  • 30
  • 58
1
vote
2 answers

Bind function pointer to boost::function object

How can I initialize a boost::function object with a raw function pointer? Metacode extern "C" { class Library { ... }; Library* createLibrary(); } ... void* functionPtr =…
Mythli
  • 5,995
  • 2
  • 24
  • 31
1
vote
1 answer

how to save any callable in c++ template class

I wanted to define a scopeguard, which runs some clean up code when scope exit. And I don't want to use std::function since it has some overhead. Then I come up with this implementation that is templated. template class…
doraemon
  • 2,296
  • 1
  • 17
  • 36
1
vote
1 answer

Can a javascript Function object be called?

This is weird! I am struggling hard with the problem that javascript Proxy handler's apply trap does not get the Proxy itself passed as an argument, only the naked function. But I need to pass along some metadata to use for re-wrapping function…
Gunther Schadow
  • 1,490
  • 13
  • 22
1
vote
2 answers

How to run do_n?

I'm currently working on recursive function on Think Python, page 44. It wrote: "Write a function called do_n that takes a function object and a number, n as arguments, and that calls the given function n times." I found a really good answer from…
JimBigMac
  • 19
  • 6
1
vote
3 answers

STL and std custom compare arguments working in c++

I asked this question on stackoverflow STL passing object I got to know that we pass objects which in tern call the compare operator in them and compare our values and gives us a result. All good. Now in this piece of code: class Compare { …
Shobhit Tewari
  • 535
  • 5
  • 20
1
vote
1 answer

How to sort a vector with given column only on a triplet value?

I have a struct with three int in it. struct x{ int a, b, c; }; I am using the struct to store triplets in the vector as the triplets will represent source, destination and weight. vector myVec; and I am adding values in it with…
vhmvd
  • 192
  • 3
  • 15
1
vote
1 answer

Using smart pointers within multi-threaded application

I have some calculation I need to perform that is dependent on two or more steps as follows: class A { public: double step1() { return 2.5; } }; class B { public: double step2() { return 1.2; } }; class Result { public: …
1
vote
2 answers

Passing function objects and using them in other functions in Python 3.8.4

I was looking at this question: cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. Given this implementation of cons: def…