user3146687 user3146687 - 2 months ago 4x
C++ Question

What is this operator overloading function overloading?

I am looking through the CMBC source code and came across this code snippet:

void goto_symext::operator()(const goto_functionst &goto_functions)
goto_functionst::function_mapt::const_iterator it=

throw "the program has no entry point";

const goto_programt &body=it->second.body;

operator()(goto_functions, body);

I have never seen a
syntax before and googling does not seem to yield anything.


That would be the function call operator, which allows you to make function-like objects.

struct Functor {
    bool operator()(int a, int b = 3);

bool Functor::operator()(int a, int b /* = 3 */) {
    if ((a > (2 * b)) || (b > (2 * a))) {
        return true;
    } else {
        return false;

// ...

Functor f;
bool b = f(5); // Calls f.operator()(5, 3).
bool c = f(3, 5);

It has the benefits that you can save state information in the function-like object's fields, and that if necessary, you can hide overloaded versions of the operator within the object instead of leaving them exposed.

class Functor {
    int compareTo;

        Functor(int cT);

        bool operator()(int a);
        bool operator()(char c);

Functor::Functor(int cT) : compareTo(cT) { }

bool Functor::operator()(int a) {
    if ((a > (2 * compareTo)) || (compareTo > (2 * a))) {
        return true;
    } else {
        return false;

bool Functor::operator()(char c) { return false; }

It can be used to make it easier to write code that can accept any function instead of depending on function pointers. [It works especially well when used in tandem with templates, in this regard.]

See here or here for more information.