How can it be useful to overload the “function cal

2019-01-11 03:26发布

I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so:

class A { 
  int n;
public: 
  void operator ()() const; 
};

And then use it this way:

A a;
a();

When is this useful?

7条回答
Ridiculous、
2楼-- · 2019-01-11 03:34

There's little more than a syntactic gain in using operator() until you start using templates. But when using templates you can treat real functions and functors (classes acting as functions) the same way.

class scaled_sine
{
    explicit scaled_sine( float _m ) : m(_m) {}
    float operator()(float x) const { return sin(m*x); }
    float m;
};

template<typename T>
float evaluate_at( float x, const T& fn )
{
   return fn(x);
}

evaluate_at( 1.0, cos );
evaluate_at( 1.0, scaled_sine(3.0) );
查看更多
贪生不怕死
3楼-- · 2019-01-11 03:35

A algorithm implemented using a template doesn't care whether the thing being called is a function or a functor, it cares about the syntax. Either standard ones (e.g. for_each()) or your own. And functors can have state, and do all kinds of things when they are called. Functions can only have state with a static local variable, or global variables.

查看更多
Emotional °昔
4楼-- · 2019-01-11 03:36

I see potential to yet one exotic use:

Suppose you have object of unknown type and have to declare another variable of same type, like this:

 auto c=decltype(a*b)(123);

When such pattern used extensively, decltype become very annoying. This case can occur when using some smart type system that automatically invent type of result of functions and operators based on types of arguments.

Now, if each specialization of each type of that type system equipped with magic definition of operator() like this:

template<????> class Num<???>{
    //specific implementation here
    constexpr auto operator()(auto...p){return Num(p...);}
}

decltype() no more needed, you can write simply:

auto c=(a*b)(123);

Because operator() of object redirects to constructor of its own type.

查看更多
三岁会撩人
5楼-- · 2019-01-11 03:46

For example for implementing generators:

// generator
struct Generator {
    int c = 0;

    virtual int operator()() {
        return c++;
    }
};

int sum(int n) {
    Generator g;

    int res = 0;
    for( int i = 0; i < n; i++ ) {
        res += g();
    }

    return res;
}
查看更多
虎瘦雄心在
6楼-- · 2019-01-11 03:47

This can be used to create "functors", objects that act like functions:

class Multiplier {
public:
    Multiplier(int m): multiplier(m) {}
    int operator()(int x) { return multiplier * x; }
private:
    int multiplier;
};

Multiplier m(5);
cout << m(4) << endl;

The above prints 20. The Wikipedia article linked above gives more substantial examples.

查看更多
Animai°情兽
7楼-- · 2019-01-11 03:51

If you're making a class that encapsulates a function pointer, this might make the usage more obvious.

查看更多
登录 后发表回答