0

Consider the following code

struct A {
    A() { /* do something */ }
};

struct B {
    B(const A& a) { /* do something*/ }
};

int main() {
    B b(A());
    // the rest of the code
}

So I intended to define b as a variable constructed from a temporary instance of A. However, compiler comprehends this line as a function prototype declaration:

warning C4930: 'B b(A (__cdecl *)(void))': prototyped function not called (was a variable definition intended?)

Can someone explain what's going on and how to do what I intended? Is this some known ambiguity resolved by the standard towards a function prototype rather than variable definition?

I know B b = A(); works, however, I don't like how it looks. It doesn't show the intent.

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • Guys, that question asks only why `A a(());` cannot be a workaround and answers suggest `A a{};`. So it's not a duplicate. Here I'm asking to explain why this happens. – Serge Rogatch May 08 '18 at 15:26
  • 1
    I'm confused: You say that it suggests `A a{};` Hmm yeah, that's exactly how you would do what you intended: `B b{A()};` – Rakete1111 May 08 '18 at 15:29
  • 1
    "Here I'm asking to explain why this happens" - It happens because the standard says that anything that *can* be interpreted as a function prototype *is* a function prototype. Using uniform initialization `B b{A()}; ` means that it cannot possibly be a function prototype, so you get what you intended. The compiler is *not* confused in this case; *your code* is. End of story. – Jesper Juhl May 08 '18 at 15:33
  • Now, knowing that the name is "most vexing parse", I was able to find my answers at https://en.wikipedia.org/wiki/Most_vexing_parse – Serge Rogatch May 08 '18 at 15:37
  • 1
    Re: "Is this some known ambiguity" -- yes, exactly. The language grammar is ambiguous, and the rule for disambiguating is that if a declaration looks like both the definition of an object and the declaration of a function, it is treated as the declaration of a function. – Pete Becker May 08 '18 at 16:46

0 Answers0