5

I was going through this article

and there is a statement in item 3 saying

// C++98 
rectangle       w( origin(), extents() );       // oops, vexing parse

how is the above a most vexing parse. If I did something like this

struct origin
{
};
struct Rectangle
{
    Rectangle(const origin& s)
    {
    }
};

The statement

 Rectangle s(origin());    

works fine and does not resemble a vexing parse. Why did the author say that its a vexing parse. Is that a typo or am I missing something ?

M.M
  • 138,810
  • 21
  • 208
  • 365
James Franco
  • 4,516
  • 10
  • 38
  • 80
  • See section 1(b) of the document, it explains these vexing parses. – Barmar Jul 22 '15 at 21:53
  • 3
    Why do you say `Rectangle s(origin());` does not resemble a vexing parse? It is the canonical example of the most vexing parse. What do you think the most vexing parse is, if not that? – Benjamin Lindley Jul 22 '15 at 21:56
  • 1
    The declaration works fine. Try to *use* `s` and see what happens. – molbdnilo Jul 22 '15 at 21:58
  • I understand that it resembles a vexing parse. However I am under the impression that a vexing parse would result in a compile time error. for example for a class foo using it like `foo a();` would give an error while compiling and its a form of most vexing parse.So from what I get is a that a statement could resemble a vexing parse and at the same time compile without any issues. A vexing parse from what I understand is a statement that might resemble a function. ` functionName (parameters..)` – James Franco Jul 22 '15 at 22:34
  • 1
    The reason we call it *vexing* is because the declaration *doesn't* cause a compile time error. It only causes an error later in the program *if* you use the function. – Aaron McDaid Jul 22 '15 at 22:36

1 Answers1

9

Rectangle s(origin()); is a vexing parse too. It declares a function s which returns rectangle, and takes as argument pointer to function returning origin. Not sure what you meant by "works fine".

rectangle w( origin(), extents() ); declares a function w returning rectangle and taking arguments: pointer to function returning origin, and pointer to function returning extents.

For more detail see this question or browse the other questions under the tag.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • By works fine I meant that it compiles with no errors. I am under the impression that a vexing parse would give a compile time error. for example for a class `foo` using it like `foo a();` would give an error while compiling and its a form of most vexing parse. – James Franco Jul 22 '15 at 22:31
  • @James Franco, not until you try to use it like it is an object – sbabbi Jul 22 '15 at 22:33
  • 4
    @JamesFranco if it were not a valid declaration, then it would not be a vexing parse. The "vexing" part is because you tried to write a variable declaration but it happened to be syntactically valid as a function declaration. Function declarations are legal, obviously. – M.M Jul 22 '15 at 22:42