views:

1502

answers:

11

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 to confusion for me.

Any insight as to the reason for this overload?

A: 

Another co-worker pointed out that it could be a way to disguise functor objects as functions. For example, this:

my_functor();

Is really:

my_functor.operator()();

So does that mean this:

my_functor(int n, float f){ ... };

Can be used to overload this as well?

my_functor.operator()(int n, float f){ ... };
JeffV
Your last line is not an operator overload at all. It needs to be: ".operator()(int n, float f)" which looks very confusing the first time you see it. You can overload this "function call operator" like like other functions, but you cannot overload it with the non-operator overload you specified.
altruic
@altruic, good point. Just fixed it.
JeffV
Your second line is wrong, it's actually "my_functor.operator()();". my_functor.operator() is the method reference, while the second set of () denotes the invocation.
eduffy
+2  A: 

You may also look over the C++ faq's Matrix example. There are good uses for doing it but it of course depends on what you are trying to accomplish.

carson
+3  A: 
MadKeithV
+5  A: 

It allows a class to act like a function. I have used it in a logging class where the call should be a function but i wanted the extra benefit of the class.

so something like this:

logger.log("Log this message");

turns into this:

logger("Log this message");
Lodle
+1  A: 

Start using std::for_each, std::find_if, etc. more often in your code and you'll see why it's handy to have the ability to overload the () operator. It also allows functors and tasks to have a clear calling method that won't conflict with the names of other methods in the derived classes.

Michel
+1  A: 

Functors are basically like function pointers. They are generally intended to be copyable (like function pointers) and invoked in the same way as function pointers. The main benefit is that when you have an algorithm that works with a templated functor, the function call to operator() can be inlined. However, function pointers are still valid functors.

Greg Rogers
+2  A: 

Many have answered that it makes a functor, without telling one big reason why a functor is better than a plain old function.

The answer is that a functor can have state. Consider a summing function - it needs to keep a running total.

class Sum
{
public:
    Sum() : m_total(0)
    {
    }
    void operator()(int value)
    {
        m_total += value;
    }
    int m_total;
};
Mark Ransom
That doesn't explain why there is a need to hide that fact that it is an object and masquerade it to be a function.
JeffV
Jeff V: Convenience. It means the same syntax can be used to make the call, whether we're calling a functor or a function pointer. If you look at std::for_each, for example, it works with either functors or function pointers, because in both cases, the syntax for the call is the same.
jalf
+29  A: 

One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it can keep data reflecting its state between calls.

Here is a simple functor example :

struct Accumulator
{
    int counter = 0;
    int operator()(int i) { return counter += i; }
}
...
Accumulator acc;
cout << acc(10) << endl; //prints "10"
cout << acc(20) << endl; //prints "30"

Functors are heavily used with generic programming. Many STL algorithms are written in a very general way, so that you can plug-in your own function/functor into the algorithhm. For example, the algorithm std::for_each allows you to apply an operation on each element of a range. It could be implemented something like that :

template <typename InputIterator, typename Functor>
void for_each(InputIterator first, InputIterator last, Functor f)
{
    while (first != last) f(*first++);
}

You see that this algorithm is very generic since it is parameterized by a function. By using the operator(), this function lets you use either a functor or a fonction pointer. Here's an example showing both possibilities :

void print(int i) { std::cout << i << std::endl; }
...    
std::vector<int> vec;
// Fill vec

// Using a functor
Accumulator acc;
std::for_each(vec.begin(), vec.end(), acc);
// acc.counter contains the sum of all elements of the vector

// Using a fonction pointer
std::for_each(vec.begin(), vec.end(), print); // prints all elements

Concerning your question about operator() overloading, well yes it is possible. You can perfectly write a functor that has several parentheses operator, as long as you respect the basic rules of method overloading (e.g. overloading only on the return type is not possible).

Luc Touraille
I think a big part of this answer is the syntax of the STL for_each. By using the operator() as the operation part of the functor it will work well with the STL.
JeffV
It seems that if the STL was implemented as do(){ ... } instead of operator()(){ ... } do would be used instead.
JeffV
Another (usually minor) advantage of functors over functions is that they can be trivially inlined. There's no pointer indirection involved, just calling a (nonvirtual) member function on a class, so the compiler can determine which function is called, and inline that.
jalf
deleted my comment on why operator() is chosen specifically, since you edited that into your post :)
jalf
Yes, thanks for the comment :-) !
Luc Touraille
'prints "10 30"': Actually, it might print "20 30". The order of evaluation of subexpressions is not specified, except that the operands of each << have to be evaluated before that << is (and hence, because of the way << associates, the order in which the parts are printed is defined). There's nothing to say that the operand of the last << must be evaluated after the operand of the first <<, just that the last << must itself be evaluated after the first. The compiler is allowed to get that acc(20) done early.
Steve Jessop
I added a sequence point so that there is no more ambiguity.
Luc Touraille
+1  A: 

The use of operator() to form functors in C++ is related to functional programming paradigms that usually make use of a similar concept: closures.

Judge Maygarden
+1  A: 

One strength I can see, however this can be discussed, is that the signature of operator() looks and behaves the same across different types. If we had a class Reporter which had a member method report(..), and then another class Writer, which had a member method write(..), we would have to write adapters if we would like to use both classes as perhaps a template component of some other system. All it would care about is to pass on strings or what have you. Without the use of operator() overloading or writing special type adapters, you couldn't do stuff like

T t;
t.write("Hello world");

because T has a requirement that there is a member function called write which accepts anything implicitly castable to const char* (or rather const char[]). The Reporter class in this example doesn't have that, so having T (a template parameter) being Reporter would fail to compile.

However, as far I can see this would work with different types

T t;
t("Hello world");

though, it still explicitly requires that the type T has such an operator defined, so we still have a requirement on T. Personally, I don't think it's too wierd with functors as they are commonly used but I would rather see other mechanisms for this behavior. In languages like C# you could just pass in a delegate. I am not too familiar with member function pointers in C++ but I could imagine you could achieve the same behaviour there aswell.

Other than syntatic sugar behaviour I don't really see the strengths of operator overloading to perform such tasks.

I am sure there are more knowingly people who have better reasons than I have but I thought I'd lay out my opinion for the rest of you to share.

Statement
The advantage of using the operator() is that your template parameter can be equally a function pointer or a functor.
Luc Touraille
+1  A: 

Other posts have done a good job describing how operator() works and why it can be useful.

I've recently been using some code that makes very extensive use of operator(). A disadvantage of overloading this operator is that some IDEs become less effective tools as a result. In Visual Studio, you can usually right-click on a method call to go to the method definition and/or declaration. Unfortunately, VS isn't smart enough to index operator() calls. Especially in complex code with overridden operator() definitions all over the place, it can be very difficult to figure out what piece of code is executing where. In several cases, I found I had to run the code and trace through it to find what was actually running.

Mr Fooz