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
0
votes
0 answers
How do I use BOOST_HOF_LIFT and BOOST_HOF_LIFT_CLASS with MSVC?
(This is in part a follow up to this question of mine.)
As I've written in this self-answer, I've discovered that Boost offers a macro to wrap a template function in a function object so it can be passed to higher-order functions:
#include…

Enlico
- 23,259
- 6
- 48
- 102
0
votes
0 answers
If I define a move constructor and don't need a copy constructor, why would I need to define a copy constructor?
Using Apple clang version 12.0.0 (clang-1200.0.32.29) -std=c++17,
the following code does not compile
struct C {
C() {}
C(C&& other) {std::cout << "move\n";}
//C(const C& other) {std::cout << "copy\n";}
void operator()(float x) {}
};
int…

pnklein
- 935
- 6
- 8
0
votes
1 answer
Why is count_if giving me the total of texts
I was testing the following code, and a bit perplexed as to why count_if is returning me the total of texts?
Match function that takes string of Text as argument and returns true is the Text size is 4
bool Match(string Text)
{
if (Text.size() ==…

dodgevipert56
- 1
- 1
0
votes
2 answers
Temporary function object in a for loop
Does the function object randomElementByWeight constructor get called for every iteration through the loop or can the compiler optimize this away somehow? I want to make sure the rand function is called for each iteration and I think it's nicer to…

ephi
- 41
- 2
0
votes
1 answer
store a function without directly creating new function objects in C++ way
I'm trying to create a generic menu, so I came with the idea of creating a menu with options, and each option in the menu will have a label, key to execute, and an Action.
The Action is template class simply stores a function and runs it when I need…

user1473213
- 63
- 4
0
votes
1 answer
Cannot find symbol of written method java.util.function
I have code like
public class Functionz {
public static boolean test() {
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {test}; // and others
for (Function func : funcs)…

ChootsMagoots
- 670
- 1
- 6
- 19
0
votes
1 answer
I wonder that if the temporary object(name_compare()) is passed by value or by reference to std::sort
I wonder that if the temporary object(name_compare()) is passed by value or by reference to std::sort.
I would be thankful for any hint on this question.
struct Record {
string name;
// ...
};
struct name_compare { //…

sunshilong369
- 646
- 1
- 5
- 17
0
votes
1 answer
Does `name_compare()` calls the default construction or the `operator()` of `class name_compare` in `sort(vs.begin(), vs.end(), name_compare());`?
Does name_compare() calls the default construction or the operator() of class name_compare in sort(vs.begin(), vs.end(), name_compare());?
I think it's the first one.Am i right?
I would be thankful for any hint on this question.
struct Record {
…

sunshilong369
- 646
- 1
- 5
- 17
0
votes
0 answers
Call function object from doIt function
#include
void hello1() {
std::cout << "Hello from normal\n";
}
auto hello2 = [] {
std::cout << "Hello from lamda\n";
};
class hello
{
public:
hello() {}
void operator()(std::string str) {
std::cout << "From functor…

Nikunj Rathod
- 1
- 2
0
votes
2 answers
Is it possible to override Function.prototype.toJSON so that JSON.stringify could work with functions?
Or maybe even override some portion of JSON.parse to parse functions?
This isn't time sensitive, I built work arounds in my code, but with the eval function, you would think that turning functions into strings and back would be a piece of cake.

Leif Messinger LOAF
- 169
- 9
0
votes
0 answers
C++: Why are function object base classes, adaptors and binders deprecated(C++11) and removed(C++17)?
Why are function object base classes (unary_function, binary_function), adaptors (pointer_to_unary_function, pointer_to_binary_function, ...) and binders(binder1st, binder2nd, ...) deprecated(C++11) and removed(C++17) ?

pasha
- 2,035
- 20
- 34
0
votes
2 answers
JavaScript Functions as Objects
I had a question regarding JavaScript Functions. I read Functions as Objects where we can add properties and methods to the functions as well but I notice some strange behavior when I log out the function with the property added. Here is a small…

Arjun
- 103
- 2
- 13
0
votes
1 answer
C++ parallel_for error
I am trying to learn how to use TBB, so I'm modifying a sample program I found that is designed to compute powers of an array of complex numbers. Originally, it was passing an array into the parallel_for loop, but I am trying to change it so that it…

Student
- 3
- 4
0
votes
1 answer
Qt5 Function object as callback copying
I tried the new Qt5 feature of using a callable object as callback.
#include
#include
#include
#include
#include
class ButtonEventHandler
{
public:
…

user877329
- 6,717
- 8
- 46
- 88
0
votes
2 answers
Real world usages of functors or function object
I am a C++ developer. Recently, I came across the concept of function objects. But I was wondering about the usages of function objects in day to day coding. Please put some light on such usage which will make the code cleaner or more efficient.

paper.plane
- 1,201
- 10
- 17