最有效解析的目的是什么?

编程入门 行业动态 更新时间:2024-10-28 06:21:16
本文介绍了最有效解析的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在维基百科上,我发现了:

A a(A());

[This]可以解释为

  • 类[ A ]的变量定义, [ A ]或

  • 函数声明返回类型为 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

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

  • 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.

    更多推荐

    最有效解析的目的是什么?

    本文发布于:2023-10-25 00:24:32,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1525411.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:目的   最有效

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!