C2x: `alignas` 作为关键字而不是宏的理由是什么?

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

C2x: what is the rationale for alignas to be a keyword rather than a macro?

问题

1990年,P.J. Plauger写道(已加重标识):

标准C为您提供了额外的安全级别。这是我所知的其他语言标准中没有的级别。它承诺,如果您避免使用某些名称集,您将不会遇到冲突。因此,标准C使编写高度可移植的应用程序变得更加容易。

在C11中,关键字_Alignas(例如)引入了伴随的宏alignas,定义在<stdalign.h>中。在这里,我们可以看到关键字是_Alignas,而不是alignas(因为在C11之前,alignas未被保留)。因此,不会与可能的用户定义的alignas发生冲突。

然而,在C2x中,alignas是一个关键字,<stdalign.h>不提供任何内容(而C2x对__alignas_is_defined宏没有提及 - 有缺陷吗?)。这意味着在C2x中,任何包含用户定义的alignas的预C2x代码都将导致语义违规,因此会破坏向后兼容性。

问题:

  1. 这是否意味着自C2x以来,“您将不会遇到冲突”不再成立?
  2. alignas(例如)作为关键字而不是宏的原因是什么?
英文:

In 1990 P.J. Plauger wrote (emphasis added):
> Standard C offers you an additional level of security, however. It is a level
offered in no other language standard that I know. It promises that if you avoid
certain sets of names, you will experience no collisions. Thus, Standard C makes
it that much easier to write highly portable applications.

In C11 keyword _Alignas (for example) was introduced with the accompanying macro alignas defined in &lt;stdalign.h&gt;. Here we see that the keyword is _Alignas, not alignas (since in pre-C11 alignas is not reserved). Hence, there is no collision with possible user-defined alignas.

However, in C2x the alignas is a keyword and &lt;stdalign.h&gt; provides no content (and C2x says nothing about __alignas_is_defined macro -- defect?). It means that in C2x any pre-C2x code containing user-defined alignas will cause semantics violation and, hence, breaks backward compatibility.

Questions:

  1. Does it mean that since C2x the "you will experience no collisions" does not hold any longer?
  2. What is the rationale for alignas (for example) to be a keyword rather than a macro?

答案1

得分: 4

以下是翻译好的部分:

提案中指出,关于新关键字的命名策略已经与之前的标准版本存在不一致之处,有些关键字使用非保留名称(如 constinline)进行集成,而其他一些则以下划线大写形式集成。对于其中一些关键字,通过一组库头文件来确保使用小写形式。

此外,提案中还提到,为了保持兼容性,将使用与C++相同的关键字命名,因为有些关键字起初源自C++语言,后来才添加到C中。

英文:

The proposal for these change (available at https://open-std.org/JTC1/SC22/WG14/www/docs/n2934.pdf) argues that the naming strategy in regards to new keywords has already been inconsistent with previous standard versions:
> some were integrated using non-reserved names (const, inline) others
were integrated in an underscore-capitalized form. For some of them, the use of the lower-case form then
is ensured via a set of library header files.

Further using the same keyword naming as C++ (for compatibility purposes) is also mentioned in the proposal, since some keywords originated in that language and were later added to C.

huangapple
  • 本文由 发表于 2023年2月8日 20:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386053.html
匿名

发表评论

匿名网友

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

确定