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
4 answers

Help understanding the working of Function Objects?

I found this code on Wikipedia class compare_class { public: bool operator()(int A, int B) const { return A < B; } }; ... // Declaration of C++ sorting function. template void sort_ints(int* begin_items, int…
Nav
  • 19,885
  • 27
  • 92
  • 135
2
votes
2 answers

How to copy references of elements of one vector to another vector?

I have a vector: std::vector sea; And now I want to hold pointers to all elements of this vector in another vector. But just for a personal exercise I want to do this in a fancy C++ style, so instead of std::vector p_sea I…
user4385532
2
votes
3 answers

How do I assign an std::function to an operator in C++?

template AVLTree::AVLTree(){ this->lessThan = Key::operator<; } This code is supposed to make the std::function lessThan field equal to the key's < operator by default. However, when I try this…
Kenkron
  • 573
  • 1
  • 3
  • 15
2
votes
3 answers

Access result type of a function template parameter in the template?

Given the following template: template class Container { private: boost::function f; }; ... and its instantiation, perhaps as follows: Container myContainer; , is there a way to access the return type of the…
Robinson
  • 9,666
  • 16
  • 71
  • 115
2
votes
2 answers

Having Function Objects Auto Detect Type in c++

I wanted to sort a vector containing ints in reverse order, but since I had gotten so used to type deduction in c++ I had passed the function object greater<>() without specifying int, since what else could it be? This did not work using g++, but…
fYre
  • 1,212
  • 3
  • 11
  • 16
2
votes
4 answers

Turn string into an object with new Function() - how does this work?

I couldn't get JSON.parse to convert a string to an object and I found this code which solves my problem - however, I can't figure out how it works. I would be grateful if someone can explain (to a JavaScript beginner) what's going on in the 3rd…
Fred
  • 73
  • 2
  • 8
2
votes
1 answer

Function object conversion to function pointer

I am looking for a way to convert function object to function pointer. Captureless lambda has implicit conversion that allows to: using fptr_t = int (*)(int); fptr_t ptr = nullptr; ptr = [](int) { return 2; }; ptr = [](auto) { return 3;…
Karol Wozniak
  • 283
  • 1
  • 7
2
votes
6 answers

understanding Functors in STL

quoting from "The C++ Standard Library" by N M Jousttis, Section 5.9 #include < iostream> #include < list> #include < algorithm> using namespace std; //function object that adds the value with which it is initialized class AddValue { …
Egon
  • 3,718
  • 3
  • 34
  • 48
2
votes
1 answer

Does Matlab support function-objects?

Trying to figure out if I have access to function object programming techniques for use in our Matlab scripts. This would be analogous to .NET's Func type, or Python's function objects. Does Matlab give functions first class object status?
jxramos
  • 7,356
  • 6
  • 57
  • 105
2
votes
2 answers

C++11: convert lambdas to function-objects in source

I have a library of C++11 code that I need to build on an older compiler that has no support for lambdas. It is not practical to manually change all the lambdas into hand-crafted function-objects. Does anyone know of a tool that I can run as a…
gimmeamilk
  • 2,016
  • 5
  • 24
  • 36
2
votes
1 answer

Dealing with function objects

std::sort is a template parameterized over a Compare class. The function itself takes an object of that class as argument and "calls" it to compare elements of the to-be-sorted container. I assume that it is important that we can give sort a…
2
votes
3 answers

How to use a function of own class in for_each method?

Assume I have this class (inherited from std::Vector, it's just an example) #include using namespace std; template class C : public vector { // I don't want to use static keyword void transformation(T i) { i…
Michal Špondr
  • 1,337
  • 2
  • 21
  • 44
2
votes
1 answer

Function objects as type fields in Julia

Is it possible to store a function object as a member/field of a composite type in Julia 0.3.x? Since it is possible to assign function objects to ordinary variables, this seems to be possible but I'm not sure how to determine the proper type (there…
Arets Paeglis
  • 3,856
  • 4
  • 35
  • 44
2
votes
1 answer

Pointer to element of std::vector

I have something like struct functor { functor(){} virtual int operator()()=0; }; struct impl : public functor { int i; impl(int ii) : i(ii) {} virtual int operator()(){return i;}; }; void call(functor& f) {std::cout << f() <<…
noobermin
  • 203
  • 1
  • 5
2
votes
2 answers

funcall in C++ : declaring functions that take functions as parameters

There are a lot of examples on the web of using STL to pass in functions or function objects as parameters, for example in std::count . How do I write my own functions that take such arguments? As a simple example, say my class is: struct Foo{ int…
kdog
  • 1,583
  • 16
  • 28