Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling a functor with an undeclared variable work? [duplicate]

class foo {
    public:
    bool operator () (int & i) {
        return true;
    }
};

int main() {
    foo(WhyDoesThisCompile);
    return 0;
}

When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.

Why is this? I tested it on clang 4.0.0.

like image 966
DanielCollier Avatar asked Apr 15 '17 20:04

DanielCollier


1 Answers

You are not invoking the functor.

You are declaring a foo, called WhyDoesThisCompile.

Yes, despite the parentheses.


I guess you meant this:

   foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
//  of   invocation of op()
// type
// `foo`

… which doesn't.

like image 85
Lightness Races in Orbit Avatar answered Nov 02 '22 15:11

Lightness Races in Orbit