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
1
vote
0 answers

Why C++ standard library does not pass predicates as &&

In C++ stl, there is e.g.: template constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p ); My question is: why there is not: ... find_if(..., UnaryPredicate &&p); In my understanding it…
Jarek C
  • 1,077
  • 6
  • 18
1
vote
1 answer

How can I compare std::function objects?

I have a vector of std::function objects defined like this: std::vector> funcs = { myFunc1, myFunc2, myFunc3 }; //The functions are defined like this: //void myFunc1() { ... } I am trying to search through this array for a…
DarkAtom
  • 2,589
  • 1
  • 11
  • 27
1
vote
0 answers

Retrieve parameter types of all overloads of callable operators of function object

#include #include struct foo { void operator()( const int& ) { } void operator()( const float& ) { } void operator()( const std::string& ) { } }; template void bar_recursive() { std::cout <<…
calynr
  • 1,264
  • 1
  • 11
  • 23
1
vote
2 answers

for_each weird behaviour

I don't use the STL much and I'm wanting to start learning it, so I made a really simple program using the STL's for_each function. Here is the entire program (minus header files): class Object { public: int s; Object() : s(0) { } …
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
1
vote
2 answers

Omitting type parameter for greater template in C++ STL

Here is a line of code I read in a book priority_queue, greater<>> min_heap; where IteratorCurrentAndEnd is a class implementing a operator> method. Why can we have greater<> instead of…
Sean
  • 2,649
  • 3
  • 21
  • 27
1
vote
1 answer

Is it wise to prefer lambdas to function objects?

After some searching and testing, I have learned the following facts about lambda expression. 1) when we write a lambda expression, the compiler would create an anonymous function object for it, and make it as an instance of the function object; 2)…
Kenan Si
  • 13
  • 2
1
vote
2 answers

Possible to pass a function into a object's method as an argument, which can refer to that object?

def my_func(): stuff return something my_object.my_method(my_func) I want my_func to be able to refer to my_object. Essentially like deferred self, which means "whatever object I am in, refer to that"
1
vote
1 answer

Terminology surrounding functors

Let's take a simple functor class LessThan { public: explicit LessThan (int in ) : val(in) {} bool operator() (int param) const { return param < val; } private: int val; }; which I can use like this for example -…
The Vivandiere
  • 3,059
  • 3
  • 28
  • 50
1
vote
2 answers

parsing JSON - eval() or function object?

To parse JSON, I believe the best method is to use native JSON support in browsers. I was looking for a good way to parse JSON in cases where native JSON support is not available. When i looked at the code in…
Anish
  • 1,164
  • 4
  • 15
  • 27
1
vote
0 answers

c++: how to pass pointer and functor as template arguments

I want to pass class pointer and a functor as a class template argument. I also dont want that that class pointer to be a local member of functor. I want to use an existing template class which is used elsewhere in the code, so I want to apply a…
bsobaid
  • 955
  • 1
  • 16
  • 36
1
vote
1 answer

Function object as template parameter

template > class LessThanPred { public: LessThanPred(const elemType &val) : _val(val){} bool operator()(const elemType &val) const { return Comp(val, _val); } …
yycfox
  • 13
  • 2
1
vote
1 answer

cout printing in reverse order

Trying overloading operator() in the following example: #include using namespace std; class Fib { public: Fib() : a0_(1), a1_(1) {} int operator()(); private: int a0_, a1_; }; int Fib::operator()() { int temp = a0_; …
KKG10
  • 11
  • 2
1
vote
2 answers

Prefer function-pointer or function-object when customizing sort and map?

I want to customizing sort template and map template in C++ Here are for comparing, struct Greater1 { bool operator() (string A, string B) { string AB = A + B; string BA = B + A; return (AB >BA); } }; static bool…
1
vote
2 answers

Scala Trait and function object

I'm starting to learn Play, and i'm stuck in trying to understand how Action is implemented. I just don't understand the language construct and that really frustrate me..... I don't understand how one can write ? val echo = Action { request =>…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
1
vote
1 answer

Which comes first, Function or Object?

There's this Object Layout diagram nicely explains the prototype chain in javascript. But it's a bit confusing to me when it comes to the relationships between the followings: function Object(){} Object.prototype function…
Aaron Shen
  • 8,124
  • 10
  • 44
  • 86