Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this declaration invoke the Most Vexing Parse?

Consider the following program:

#include <fstream>

struct A {};

int main(int argc, char** argv) {
    A a(std::fstream(argv[1]));
}

Clang in C++1y mode reckons that the MVP is invoked such that a is parsed as a function declaration:

clang++ -std=c++1y -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out

main.cpp:6:8: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]

    A a(std::fstream(argv[1]));

       ^~~~~~~~~~~~~~~~~~~~~~~

main.cpp:6:9: note: add a pair of parentheses to declare a variable

    A a(std::fstream(argv[1]));

        ^

        (                    )

I understand the MVP, but not in this instance: argv[1] is clearly an expression, and there's no type before it, so how could this line be parsed as a function declaration?

Is it that the semantic interpretation on argv[1], that would disambiguate the line as an object declaration, doesn't occur until after the compiler has already chosen to parse the line as a function declaration? Or is it a Clang bug? Or is it perfectly normal through some interpretation of the tokens argv [ 1 ] that I'm missing?

like image 982
Lightness Races in Orbit Avatar asked Feb 07 '14 10:02

Lightness Races in Orbit


1 Answers

I think it's being parsed as

A a(std::fstream argv[1]);

i.e. a function that takes in an array of 1 std::fstream, where the extra parentheses are redundant.

Of course, in reality, that array parameter decays to a pointer, so what you end up with semantically is:

A a(std::fstream* argv);
like image 185
user541686 Avatar answered Sep 24 '22 06:09

user541686