Questions tagged [function-call-operator]

In C++, function call operator is a construct that allows object to be used like it were a function. This feature in general is known as function object (functor).

20 questions
1061
votes
14 answers

What are C++ functors and their uses?

I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?
Konrad
  • 39,751
  • 32
  • 78
  • 114
82
votes
12 answers

Why override operator()?

In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led…
JeffV
  • 52,985
  • 32
  • 103
  • 124
24
votes
3 answers

Why is a lambda's call-operator implicitly const?

I have a small "lambda expression" in the below function: int main() { int x = 10; auto lambda = [=] () { return x + 3; }; } Below is the "anonymous closure class" generated for the above lambda expression. int main() { int x = 10; …
cpp_enthusiast
  • 1,239
  • 10
  • 28
16
votes
2 answers

In a function call, what is the operator, and what are the operands?

I am trying to understand some basics of C. KRC's The C Programming Language says A function call is a postfix expression, called the function designator, followed by parentheses containing a possibly empty, comma-separated list of …
Tim
  • 1
  • 141
  • 372
  • 590
11
votes
4 answers

How do I implement the Fn trait for one struct for different types of arguments?

I have a simple classifier: struct Clf { x: f64, } The classifier returns 0 if the observed value is smaller than x and 1 if bigger than x. I want to implement the call operator for this classifier. However, the function should be able to take…
asdetrefle
  • 303
  • 1
  • 6
  • 14
7
votes
2 answers

Function call operator

Possible Duplicates: C++ Functors - and their uses. Why override operator() ? I've seen the use of operator() on STL containers but what is it and when do you use it?
george
  • 73
  • 1
  • 1
  • 3
4
votes
1 answer

Passing a static operator() as deleter type

Is the following code snippet legal in C++23? #include #include int main() { struct custom_deleter { static void operator()(int* const ptr) { delete ptr; std::fputs( "Deleted\n",…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
3
votes
4 answers

What does void Classname::operator()(){ .... } do?

Im working my way through some C++ code and came across the following void Classname::operator()() { //other code here } I assume this has something to do with overloading the constructor, but can someone elaborate on that?
madmaze
  • 878
  • 3
  • 11
  • 23
2
votes
2 answers

overload assignment and round bracket operator in C++

I want to define a class myVector that support both assignment operator= and bracket access e.g myclass(1) = 0.5. See a dummy example below class myVector { public: vector _v; myVector(unsigned int size) : _v(size, 0) { } …
Peter Trade
  • 21
  • 1
  • 2
2
votes
1 answer

Is there a way to spawn a std::thread using an object and its non-empty argument list call operator?

I'm new to std::thread and C++11 in general. Trying to toy with the examples from https://en.cppreference.com/w/cpp/thread/thread/thread, I am trying to see if I can spawn a std::thread using a class member function call operator with non-empty…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
2
votes
2 answers

How to Implicitly Call a Function Call Operator?

How does one implicitly call a class object's templated function call operator? class User_Type { public: template< typename T > T operator()() const; }; void function() { User_Type user_var; int int_var_0 = user_var.operator()< int…
Charles L Wilcox
  • 1,126
  • 8
  • 18
2
votes
2 answers

Overload function-call operator and return type

I'm designing a little mathematical library. I have classes that represents classes of function, e.g. polynomial. When they're instantiated, the parameters needed (a1, a2, a3) are provided so the object actually represent a specific polynomial…
1
vote
2 answers

Passing a reference to template function call operator overload

I have a class which overloads the function call operator with a template function, like so: class Test { public: template void operator()(T t) { std::cout<<(&t)<
1
vote
1 answer

C++ - Template function call operator overload - Error C2064

I'm trying to overload function call operator in c++ and I got this compilation error that I cannot resolve (Visual Studio 2010). Error is in line act(4); #include #include void Test(int i); template class Action { …
EOG
  • 1,677
  • 2
  • 22
  • 36
0
votes
0 answers

How to overload the function call operator based on whether called from the left-hand or right-hand side

Is it possible to const-overload the function call operator operator() so that: when the caller is on the left-hand side of an expression, the non-constant version is always called when the caller is on the right-hand side of the expression, the…
1
2