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

1

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.

Vickers answered 15/4, 2017 at 20:15 Comment(15)
@TheGreatDuck there is no foo function... foo is a class. please read accepted answerVickers
@TheGreatDuck no. there is no other function called foo. this code compiles as is. see godbolt.org/g/4tZAS0 for exampleVickers
@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.Vickers
@TheGreatDuck again. see link godbolt.org/g/4tZAS0Vickers
@TheGreatDuck why would a constructor accept a "WhyDoesThisCompile" when it doesnt exist."Foo(WhyDoesThisCompile)" == "Foo WhyDoesThisCompile;"Vickers
@TheGreatDuck i still don't think you understand. this should clear things up godbolt.org/g/ZbfqrBVickers
@TheGreatDuck your comments make no sense. Default constructors don't accept "a type of undeclared variable" (whatever that is even supposed to mean)Dukey
@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.Plasmagel
@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?Gutenberg
@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 pointDukey
@TheGreatDuck: Which is (a) completely irrelevant, and (b) not what you were claimingGutenberg
@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."Gutenberg
@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.Gutenberg
@TheGreatDuck: The... "declaration operator"? What are you prattling on about now?Gutenberg
Then I guess we have nothing further to say. I shall simply refer to you all the above responses to your nonsensical claims! Good nightGutenberg
G
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.

Gutenberg answered 15/4, 2017 at 20:18 Comment(9)
so this is the same as, code -> "foo WhyDoesThisCompile;" ? if so, i find that really odd.Vickers
Insanely confusing and counter-intuitive. Didn't believe it was legal C++.Staciastacie
@BoundaryImposition I ran into this syntax accedentally i was confused that it was still compiling, thanks for the answer!Vickers
@DanielCollier: No problem - enjoy the bank hol!Gutenberg
Any idea which part of the standard grammar makes this possible? I can't seem to locate it.Staciastacie
@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);Jubilant
Every few weeks, we get one of those ambiguity/vexing parse questions :)Illboding
@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 sameDukey
Just when I thought I knew everything about C++ syntax...Wildebeest

© 2022 - 2024 — McMap. All rights reserved.