在表达式陈述开始时,`是``auto(expr)'被视为铸件吗?

发布于 2025-01-26 19:14:31 字数 1750 浏览 3 评论 0原文

我有一个简单的代码段( httpps://godbolt.org/z/cpt​​3phydj ):

int main() {
  int x = 1;
  auto(1); // ok in GCC, error in Clang
  auto{1}; // ok in GCC, error in Clang
  static_cast<void>(auto(x)); // ok
  auto{x}; // ok in GCC, error in Clang
  auto(x); // both error in GCC an Clang
}

GCC和Clang都会发出错误:

// by GCC Trunk [-std=c++23]
<source>: In function 'int main()':
<source>:7:3: error: declaration of 'auto x' has no initializer
    7 |   auto(x); // why
      |   ^~~~
Compiler returned: 1

// by Clang Trunk [-std=c++2b]
<source>:3:8: error: expected unqualified-id
  auto(1); // why
       ^
<source>:3:8: error: expected ')'
<source>:3:7: note: to match this '('
  auto(1); // why
      ^
<source>:4:7: error: expected unqualified-id
  auto{1}; // why
      ^
<source>:6:7: error: expected unqualified-id
  auto{x}; // why
      ^
<source>:7:8: error: redefinition of 'x'
  auto(x); // why
       ^
<source>:2:7: note: previous definition is here
  int x = 1;
      ^
<source>:7:8: error: declaration of variable 'x' with deduced type 'auto' requires an initializer
  auto(x); // why
       ^
6 errors generated.
Compiler returned: 1
  • 如果C ++ 23是实验性的,并且由于引入了另一个auto(expr),他们是否能够修复歧义或更改歧义。还是离开它?

  • 这些表达式是否应该被解析为显式类型的衰减转换auto(expr)auto {expr}在表达式语句中或以声明为单位?

  • 如果没有歧义,则首先要优先考虑:

    • auto(标识符) AS 自动标识符?或
    • auto(标识符)作为铸造表达式?

I have a simple code snippet shown below (https://godbolt.org/z/cPT3PhYdj):

int main() {
  int x = 1;
  auto(1); // ok in GCC, error in Clang
  auto{1}; // ok in GCC, error in Clang
  static_cast<void>(auto(x)); // ok
  auto{x}; // ok in GCC, error in Clang
  auto(x); // both error in GCC an Clang
}

Where both GCC and Clang emit an error showing:

// by GCC Trunk [-std=c++23]
<source>: In function 'int main()':
<source>:7:3: error: declaration of 'auto x' has no initializer
    7 |   auto(x); // why
      |   ^~~~
Compiler returned: 1

// by Clang Trunk [-std=c++2b]
<source>:3:8: error: expected unqualified-id
  auto(1); // why
       ^
<source>:3:8: error: expected ')'
<source>:3:7: note: to match this '('
  auto(1); // why
      ^
<source>:4:7: error: expected unqualified-id
  auto{1}; // why
      ^
<source>:6:7: error: expected unqualified-id
  auto{x}; // why
      ^
<source>:7:8: error: redefinition of 'x'
  auto(x); // why
       ^
<source>:2:7: note: previous definition is here
  int x = 1;
      ^
<source>:7:8: error: declaration of variable 'x' with deduced type 'auto' requires an initializer
  auto(x); // why
       ^
6 errors generated.
Compiler returned: 1
  • If C++23 is experimental, and will they be able to fix the ambiguity or change the disambiguation since another auto(expr) is introduced, or just leave it be?

  • Are these expressions supposed to be parsed as explicit type decay conversion auto(expr) or auto{expr} in expression statements or parsed as a declaration?

  • If there is no ambiguity, then which priority comes first:

    • auto(identifier) as auto identifier?, or
    • auto(identifier) as cast expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2025-02-02 19:14:31

来自显式铸造转换

  auto(表达式)(8)(自C ++ 23)

auto {expression}(9)(由于C ++ 23)
 

8,9)autoauto> auto x(expression);声明的发明变量x的定义类型替换了指定词。 (从未解释为函数声明)或auto x {expression};。结果始终是对象类型的prvalue。

因此,似乎(按照上述语句允许您使用)。

在这里是您代码的工作演示。注意在链接的演示中,仅使用auto(x)会产生错误,所有其他情况都可以正常工作。


另请注意,从 pr105516 ::::::

auto(x);被解释为auto x;。使用+auto(x);如果您希望它是表达式。


如果没有歧义,则首先要优先考虑:

这是一个示例之一,显示了C ++是上下文敏感语言。如果不知道其更广泛的上下文,就不能总是理解构造。考虑以下示例:

int main()
{
    int x = 0   ;
    int k       ;
//------vvvvvvv----->here auto(x) is behaves as an expression as it is used as an expression 
    k = auto(x) ;

    auto(p)     ;  //this a declaration and not an explicit case unlike the above
}

From Explicit cast conversion:

auto ( expression )   (8)     (since C++23)

auto { expression }   (9)     (since C++23)

8,9) The auto specifier is replaced with the deduced type of the invented variable x declared with auto x(expression); (which is never interpreted as a function declaration) or auto x{expression}; respectively. The result is always a prvalue of an object type.

So your usage seems to be allowed(in accordance with ) by the above quoted statement.

Here is a working demo of your code. Note in the linked demo, only the usage auto(x) produces an error, all other cases work fine.


Also note that from PR105516:

auto(x); is interpreted as auto x;. Use +auto(x); if you want that to be an expression.


If there is no ambiguity, then which priority comes first:

This is one of the examples showing how C++ is a context sensitive language. A construct cannot always be understood without knowing its wider contexts. Consider the following example:

int main()
{
    int x = 0   ;
    int k       ;
//------vvvvvvv----->here auto(x) is behaves as an expression as it is used as an expression 
    k = auto(x) ;

    auto(p)     ;  //this a declaration and not an explicit case unlike the above
}
べ映画 2025-02-02 19:14:31

据我所知A>引入此功能没有任何进一步的相关更改,除了允许auto作为功能风格的显式铸件中的类型说明器。

因此,直观地自动在这里的行为应与任何其他类型的规范符相同。

例如,用auto int 替换,预计所有这些情况都可以正常工作并且是功能上的显式

int(x); // auto(x);

演员声明一个名为x的变量 int (或占位符类型),并带有声明器周围的括号,而没有初始化器。

与往常一样,语法是通过偏爱解释为声明的(参见 .ambig]/1 )。至少,我认为对于auto,它应该没有不同。具有auto占位符类型的声明需要一个初始化器,但在此解释中不存在,而是根据 [stmt.ambig]/3 即使歧义也应很早,纯粹是句法的,即使它以不良形式的声明结束。我不完全确定,但我想这应该意味着auto(x);仍然应该被视为声明。

我不知道为什么Clang给其他许多行都会出现错误。我想该功能要么仅部分实现,要么是实现错误(这是未完成的标准修订中的一个非常新的功能)。

As far as I can tell the paper introducing this feature didn't make any further relevant changes except to allow auto as type specifier in a functional-style explicit cast.

So intuitively auto here should behave the same as any other type specifier would.

For example, replacing auto with int, it is expected that all of these cases work and are functional-style explicit casts, except

int(x); // auto(x);

This one could according to the grammar also be a declaration of a variable named x of type int (or placeholder type) with parentheses around the declarator and without initializer.

As usual, the grammar is disambiguated by preferring the interpretation as declaration (see [stmt.ambig]/1). At least, I don't think that it should be different for auto. A declaration with auto placeholder type requires an initializer, which is not present in this interpretation, but according to [stmt.ambig]/3 the disambiguation is supposed to be done very early and purely syntactic even if it ends up in an ill-formed declaration. I am not completely sure but I guess this should mean that auto(x); should still be disambiguated as a declaration.

I don't know why Clang gives errors for many of the other lines. I suppose the feature is either implemented only partially or these are implementation bugs (it is a very new feature in an unfinished standard revision).

寄人书 2025-02-02 19:14:31

我认为auto(x)(其中x实际上是标识符)仍然可以在不同的上下文中具有不同的含义,因此受 [dcl.ambig.res] 在C ++ 23中。

不幸的是,歧义不太可能被修复。在以下程序中(自C ++ 11以来良好的程序),auto(x)= bar {};被解释为声明。如果auto(x)被“修复”,则auto(x)= bar {};将成为一个表达式语句,它将改变现有行为。

#include <cstdio>
struct Bar {
    Bar()
    {
        std::puts("initialization");
    }
};

struct Foo {
    void operator=(Bar)
    {
        std::puts("assignment");
    }
};

int main()
{
    Foo x{};
    {
        auto(x) = Bar{}; // only prints "initialization"
    }
}

I think auto(x) (where x is literally an identifier) can still have different meanings in different contexts and thus is subject to [dcl.ambig.res] in C++23.

Unfortunately, the ambiguity is unlikely to be fixed. In the following program (well-formed since C++11), auto(x) = Bar{}; is interpreted as a declaration. If auto(x) were "fixed", auto(x) = Bar{}; would become an expression statement, which would change the existing behavior.

#include <cstdio>
struct Bar {
    Bar()
    {
        std::puts("initialization");
    }
};

struct Foo {
    void operator=(Bar)
    {
        std::puts("assignment");
    }
};

int main()
{
    Foo x{};
    {
        auto(x) = Bar{}; // only prints "initialization"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文