C23 的 auto 与 C++11 的 auto

huangapple go评论59阅读模式
英文:

C23 auto vs C++11 auto

问题

The C23标准显然已经引入了使用"auto"关键字进行自动类型推导,就像在C++11中一样。然而,似乎存在一些差异。

根据这里,https://en.cppreference.com/w/cpp/keyword/auto,在C++11之后,auto不再是C++中的存储期说明符。

但是,我无法轻易找到C23的等效声明。在C23中,auto仍然是C中的存储类说明符吗?

我们是否仍然可以写成 int auto x = 1; 在C23中?

**编辑:**第一个问题的答案是肯定的。但正如下面评论中的Andrew Henle指出的那样,第二个问题有所不同:

我们是否仍然可以写成 float auto x = 1; 在C23中?

正如@AndrewHenle和@VladfromMoscow引用的标准文档中所述,6.7.1存储类说明符,第4段:

auto只能出现在具有文件作用域的标识符的声明说明符中,或者如果类型要从初始化程序中推断,则可以与其他存储类说明符一起出现。

似乎这不包括float auto x = 1;的情况,如果此声明不在文件作用域内。

这是什么解释?

还有另一个问题:这个句子似乎令人困惑,因为我们肯定可以在没有“其他存储说明符”的情况下使用auto,不是吗?比如auto a = 1;

英文:

The C23 standard apparently has introduced using "auto" keyword for auto type deduction, see here, just like in C++11. However, there seems to be some differences.

According to here, https://en.cppreference.com/w/cpp/keyword/auto, after C++11, auto is no longer a storage duration specifier in C++.

However, I cannot easily find an equivalently statement for C23. Is it the case that auto is still a storage class specifier in C in C23?

Can we still write int auto x = 1; in C23?

EDIT: The answer to the first question is yes. But as pointed out by Andrew Henle in comments below, the second question is different:

Can we still write float auto x = 1; in C23?

As quoted by @AndrewHenle and @VladfromMoscow, in the standard document, 6.7.1 Storage-class specifiers, paragraph 4

> auto shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.

It seems that this does not cover the case float auto x = 1;, if this declaration is not in file scope.

What's the interpretation of this?

There is another question: the sentence seems confusing because we surely can use auto without "other storage specifiers", couldn't we? Like auto a = 1;.

答案1

得分: 6

是的,auto 仍然是 C23 中的存储类说明符:

参见 6.7.1 存储类说明符

  • auto
  • constexpr
  • extern
  • register
  • static
  • thread_local
  • typedef
英文:

Yes, auto is still a storage-class specifier in C23:

See 6.7.1 Storage-class specifiers:

  • auto
  • constexpr
  • extern
  • register
  • static
  • thread_local
  • typedef

答案2

得分: 3

In C23, "auto"仍然是一种存储类别说明符。

来自C23 (6.7.1 存储类别说明符)

语法:

1 存储类别说明符:
auto
constexpr
extern
register
static
thread_local
typedef

语义

6 存储类别说明符指定标识符和声明的特性:

— 存储期 (在块作用域中的静态、线程局部、auto、register),

4 thread_local 不应出现在函数声明的声明说明符中。auto 只能出现在文件作用域标识符的声明说明符中,或者与其他存储类别说明符一起,如果类型是从初始化器中推断出的

英文:

In C23 auto is still a storage class specifier.

From the C 23 (6.7.1 Storage-class specifiers)

Syntax

1 storage-class-specifier:
    auto
    constexpr
    extern
    register
    static
    thread_local
    typedef

and

> Semantics
>
> 6 Storage-class specifiers specify various properties of identifiers
> and declared features:
>
> — storage duration (static in block scope, thread_local, auto,
> register),
>
> 4 thread_local shall not appear in the declaration specifiers of a
> function declaration. auto shall only appear in the declaration
> specifiers of an identifier with file scope or along with other
> storage class specifiers if the type is to be inferred from an
> initializer

答案3

得分: 2

float auto x = 1; 是有效的,并且与 C23 之前的意思相同(这里的 auto 和 C23 之前一样多余)。工作草案中没有迹象表明可能不是这种情况。

auto 只能出现在具有文件作用域的标识符的声明说明符中,或者与其他存储类说明符一起出现,如果类型要从初始化程序中推断出来。

这并不适用于函数作用域中的 float auto x = 1;。在这个声明中,auto 仍然指定了存储期,因为:

存储类说明符指定了标识符和声明特性的各种属性:

  • 存储期(在块作用域中为 static,thread_local,auto,register),

而且 auto 的这个含义不会被忽略,因为

如果 auto 与另一个存储类说明符一起出现,或者它出现在文件作用域的声明中,那么在确定存储期或链接性方面,将其忽略

auto 并没有指定类型应该从初始化程序中推断出来,因为

对于这样的声明,其中声明说明符不包含类型说明符,从初始化程序中推断类型的机制在 6.7.9 中进行了讨论。

英文:

float auto x = 1; is valid and means the same thing as in pre-C23 (and auto here is as redundant as in pre-C23). There is no indication in the working draft that it might not be the case.

> auto shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.

This does not cover float auto x = 1; at a function scope. In this declaration, auto still specifies storage duration because:

> Storage-class specifiers specify various properties of identifiers and declared features:
> - storage duration (static in block scope, thread_local, auto, register),

and this meaning of auto is not ignored because

> If auto appears with another storage-class specifier, or if it appears in a declaration at file scope, it is
ignored for the purposes of determining a storage duration or linkage

and auto does not specify that the type shall be inferred because

> For a declaration such that the declaration specifiers contain no type specifier a mechanism to infer the type from an initializer is discussed in 6.7.9

huangapple
  • 本文由 发表于 2023年5月15日 01:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定