21
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.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
DanielCollier
  • 736
  • 2
  • 8
  • 21
  • 1
    @TheGreatDuck there is no foo function... foo is a class. please read accepted answer – DanielCollier Apr 15 '17 at 21:00
  • @TheGreatDuck no. there is no other function called foo. this code compiles as is. see https://godbolt.org/g/4tZAS0 for example – DanielCollier Apr 15 '17 at 21:44
  • @TheGreatDuck dude, it is not a defaut constructor call. did you even read the answer? "You are declaring a foo, called WhyDoesThisCompile. Yes, despite the parentheses" see my link to compiler explorer to prove that it compiles. – DanielCollier Apr 15 '17 at 21:49
  • @TheGreatDuck again. see link https://godbolt.org/g/4tZAS0 – DanielCollier Apr 15 '17 at 21:51
  • @TheGreatDuck why would a constructor accept a "WhyDoesThisCompile" when it doesnt exist."Foo(WhyDoesThisCompile)" == "Foo WhyDoesThisCompile;" – DanielCollier Apr 15 '17 at 21:56
  • @TheGreatDuck i still don't think you understand. this should clear things up https://godbolt.org/g/ZbfqrB – DanielCollier Apr 15 '17 at 22:14
  • 5
    @TheGreatDuck your comments make no sense. Default constructors don't accept "a type of undeclared variable" (whatever that is even supposed to mean) – M.M Apr 16 '17 at 03:09
  • 9
    @TheGreatDuck the lack of understanding isn't that what you're saying isn't clear, it's that it's **clearly 100% wrong** and you keep repeating it in the face of evidence, and it's hard to understand **why** you keep insisting on adding incorrect nonsense to a question that's already been adequately answered. – hobbs Apr 16 '17 at 05:56
  • 3
    @TheGreatDuck: No, it is the syntax of a declaration, as has now been explained and demonstrated multiple times. There is no function call here whatsoever. And, no, there is no such thing as type "undefined" in C++. Where did you hear that there is? – Lightness Races in Orbit Apr 16 '17 at 11:29
  • 4
    @TheGreatDuck there is no such type as "undefined" in C++ ; and the syntax `a(b)` has several possible meanings depending what `a` and `b` are. Also there is no built-in function called `foo`. You must just be trolling at this point – M.M Apr 16 '17 at 12:01
  • @TheGreatDuck: Which is (a) completely irrelevant, and (b) not what you were claiming – Lightness Races in Orbit Apr 17 '17 at 01:43
  • 1
    @TheGreatDuck: No, it isn't. I shall now quote you: _"Where is this 'foo' function that you are calling?" " You miss my point. Somewhere in the code, you have made a function call to a function called foo" "Dude... you're either invoking the default constructor or something within the C++ language. That is definitely a function call." "There is a function somewhere. It is probable just an implicitly defined function." "I'm saying it is a call to the default constructor." "it's the syntax of a function call. Clearly c++ is intending it to mean something in terms of function calling."_ – Lightness Races in Orbit Apr 17 '17 at 13:18
  • 1
    @TheGreatDuck: _(cont.)_ _"There is such a thing as type 'undefined' in c++" "How could there not be an undefined type? How else does c++ recognize when something is undefined..."_ These are your claims, and every single one of them is wrong. – Lightness Races in Orbit Apr 17 '17 at 13:18
  • 1
    @TheGreatDuck: The... "declaration operator"? What are you prattling on about now? – Lightness Races in Orbit Apr 18 '17 at 01:26
  • 1
    Then I guess we have nothing further to say. I shall simply refer to you all the above responses to your nonsensical claims! Good night – Lightness Races in Orbit Apr 18 '17 at 01:27

1 Answers1

30

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    so this is the same as, code -> "foo WhyDoesThisCompile;" ? if so, i find that really odd. – DanielCollier Apr 15 '17 at 20:24
  • 8
    Insanely confusing and counter-intuitive. Didn't believe it was legal C++. – DeiDei Apr 15 '17 at 20:24
  • @BoundaryImposition I ran into this syntax accedentally i was confused that it was still compiling, thanks for the answer! – DanielCollier Apr 15 '17 at 20:32
  • 2
    @DanielCollier: No problem - enjoy the bank hol! – Lightness Races in Orbit Apr 15 '17 at 20:33
  • Any idea which part of the standard grammar makes this possible? I can't seem to locate it. – DeiDei Apr 15 '17 at 20:41
  • 9
    @DanielCollier, The syntax comes from C, but in general, you can put extra parentheses in declarations. You sometimes need them, too, if not using type aliases or other tricks: `int(&arr)[2];` or `void(*f)(int);` – chris Apr 15 '17 at 20:41
  • 10
    Every few weeks, we get one of those ambiguity/vexing parse questions :) – T.C. Apr 15 '17 at 22:34
  • 4
    @DeiDei see [dcl.decl]/4 where it gives the grammar for declarations - a `noptr-declarator` is a `ptr-declarator`, and `( ptr-declarator )` is a `noptr-declarator`, which is saying that you can put parentheses around any declarator. For example `int *p;` and `int (((*(((((p))))))));` are the same – M.M Apr 16 '17 at 03:13
  • 1
    Just when I thought I knew everything about C++ syntax... – Alexander Revo Apr 16 '17 at 09:54