这个声明如何调用Most Vexing Parse? [英] How does this declaration invoke the Most Vexing Parse?

查看:322
本文介绍了这个声明如何调用Most Vexing Parse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下程序:

#include <fstream>

struct A {};

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

在C ++ 1y模式中的Clang假定MVP被调用, a href =http://coliru.stacked-crooked.com/a/393a356e98041cf2> a 被解析为函数声明:

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]));

        ^

        (                    )



MVP,但不是在这种情况下: 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?

argv [1] 的语义解释,它会将行解释为对象声明,直到编译器已经选择将该行解析为函数声明之后才会发生?或者是一个Clang bug?或者是通过对标记 argv [1] 的一些解释,我错过了吗?

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?

推荐答案

我认为它被解析为

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

一个函数接受1 std :: fstream 的数组,其中额外的括号是多余的。

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);

这篇关于这个声明如何调用Most Vexing Parse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆