Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the Most Vexing Parse?

On Wikipedia I found this:

A a( A() ); 

[This] could be disambiguated either as

  1. a variable definition of class [A], taking an anonymous instance of class [A] or
  1. a function declaration for a function which returns an object of type [A] and takes a single (unnamed) argument which is a function returning type [A] (and taking no input).

Most programmers expect the first, but the C++ standard requires it to be interpreted as the second.

But why? If the majority of the C++ community expects the former behavior, why not make it the standard? Besides, the above syntax is consistent if you don't take into account the parsing ambiguity.

Can someone please enlighten me? Why does the standard make this a requirement?

like image 581
template boy Avatar asked Dec 29 '12 00:12

template boy


1 Answers

Let's say MVP didn't exist.

How would you declare a function?

A foo(); 

would be a variable definition, not a method declaration. Would you introduce a new keyword? Would you have a more awkward syntax for a function declaration? Or would you rather have

A foo; 

define a variable and

A foo(); 

declare a function?

Your slightly more complicated example is just for consistency with this basic one. It's easier to say "everything that can be interpreted as a declaration, will be interpreted as a declaration" rather than "everything that can be interpreted as a declaration, will be interpreted as a declaration, unless it's a single variable definition, in which case it's a variable definition".

This probably isn't the motivation behind it though, but a reason it's a good thing.

like image 162
Luchian Grigore Avatar answered Oct 03 '22 12:10

Luchian Grigore