最有效解析的目的是什么? [英] What is the purpose of the Most Vexing Parse?

查看:91
本文介绍了最有效解析的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科上,我发现了:

  A a(A()); 




[This]可以解释为


  1. 类[ A ]的变量定义, [ A ]或


  2. 函数声明返回类型为< c $ c> A ],并接受一个(未命名)参数,它是一个返回类型[ A ]


大多数程序员都期望第一个,但C ++标准要求它被解释为第二个。 / strong>


但是为什么?如果大多数C ++社区期望以前的行为,为什么不使它成为标准?此外,如果您考虑解析歧义,上述语法是一致的。



有人可以启发我吗?

假设MVP不存在。 / p>

如何声明一个函数?

  

将是一个变量定义,而不是一个方法声明。您会介绍一个新的关键字吗?你会有一个更尴尬的语法的函数声明?或者你宁愿有

  A foo; 

定义变量和

  A foo(); 

声明一个函数?



更复杂的例子只是为了与这个基本的一致。更容易说一切可以被解释为声明,将被解释为一个声明而不是一切可以解释为一个声明,将被解释为一个声明,除非是单个变量定义,在这种情况下,它是一个变量定义



这个可能但它是一个的东西。


On Wikipedia I found this:

A a( A() );

[This] could be disambiguated either as

  1. a variable definition of class [A], taking an anonymous instance of class [A] or

  2. a function declaration for a function which returns an object of type [A] and takes a single (unnamed) argument which is a function returning type [A] (and taking no input).

Most programmers expect the first, but the C++ standard requires it to be interpreted as the second.

But why? If majority of the C++ community expects the former behavior, why not make it the standard? Besides, the above syntax is consistent if you don't take into account the parsing ambiguity.

Can someone please enlighten me? Why does the standard make this a requirement?

解决方案

Let's say MVP didn't exist.

How would you declare a function?

A foo();

would be a variable definition, not a method declaration. Would you introduce a new keyword? Would you have a more awkward syntax for a function declaration? Or would you rather have

A foo;

define a variable and

A foo();

declare a function?

Your slightly more complicated example is just for consistency with this basic one. It's easier to say "everything that can be interpreted as a declaration, will be interpreted as a declaration" rather than "everything that can be interpreted as a declaration, will be interpreted as a declaration, unless it's a single variable definition, in which case it's a variable definition".

This probably isn't the motivation behind it though, but a reason it's a good thing.

这篇关于最有效解析的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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