How does this declaration invoke the Most Vexing Parse?

后端 未结 2 1540
有刺的猬
有刺的猬 2020-12-07 01:03

Consider the following program:

#include 

struct A {};

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

相关标签:
2条回答
  • 2020-12-07 02:00

    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);
    
    0 讨论(0)
  • 2020-12-07 02:01

    The parens are superfluous. All of the following are the declarations:

    T a;
    T (a);
    T ((a));
    T (((a))));
    

    so are these:

    T a[1];
    T (a[1]);
    T ((a[1]));
    T (((a[1]))));
    

    So is this:

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

    which is same as:

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

    which is same as:

     A a(std::fstream *argv);
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题