How is this a most vexing parse?
Asked Answered
R

1

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 ?

Roper answered 22/7, 2015 at 21:47 Comment(5)
See section 1(b) of the document, it explains these vexing parses.Fistula
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?Hyaline
The declaration works fine. Try to use s and see what happens.Thundery
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. <Return type> functionName (parameters..)Roper
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.Heterosexual
L
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.

Laurasia answered 22/7, 2015 at 21:54 Comment(3)
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.Roper
@James Franco, not until you try to use it like it is an objectLatchkey
@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.Laurasia

© 2022 - 2024 — McMap. All rights reserved.