Why override operator()?

2019-01-03 11:50发布

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?

11条回答
孤傲高冷的网名
2楼-- · 2019-01-03 12:48

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.

查看更多
萌系小妹纸
3楼-- · 2019-01-03 12:49

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.

查看更多
Animai°情兽
4楼-- · 2019-01-03 12:53

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.

查看更多
Root(大扎)
5楼-- · 2019-01-03 12:55

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.

查看更多
仙女界的扛把子
6楼-- · 2019-01-03 12:55

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){ ... };
查看更多
登录 后发表回答