c++ - 寄存器变量地址

标签 c++ c keyword register-allocation

在 C 中,我们不能使用 & 来找出寄存器变量的地址,但在 C++ 中我们可以这样做。为什么它在 C++ 中合法,但在 C 中不合法?有人可以深入解释这个概念。

最佳答案

这是 C99 standard (pdf) 的第 6.7.1 节(脚注 101)的摘录:

The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1). Thus, the only operator that can be applied to an array declared with storage-class specifier register is sizeof.

从第 7.1.1 节第 3 段的 C++ standard (pdf) :

A register specifier has the same semantics as an auto specifier together with a hint to the implementation that the object so declared will be heavily used. [Note: the hint can be ignored and in most implementations it will be ignored if the address of the object is taken. —end note]

关于注册的有趣花絮

C++ 组 (WG21) wants to deprecate register :

The register keyword serves very little function, offering no more than a hint that a note says is typically ignored. It should be deprecated in this version of the standard, freeing the reserved name up for use in a future standard, much like auto has been re-used this time around for being similarly useless.

Notes from the March, 2009 meeting:

The consensus of the CWG was in favor of deprecating register.

看看C99组(WG14)said about register (pdf)在 session 上:

General agreement to deprecate the “auto” keyword. Should we ask WG21 to go back to the previous use of “register” (no address)? No, this will not fly with WG21.

关于c++ - 寄存器变量地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1256246/

相关文章:

c++ - 何时在 C++ 中使用 friend 类

java - Java中有goto语句吗?

c++ - 嵌套类是否获得外部类的友元关系?

c++ - 确定动态库是否存在的多平台方法

c++ - C++类中的函数指针

c++ - 如何正确进行具有大量依赖项的单元测试

c - 调试相当于 C 代码的程序集以了解函数调用

c - 使用 GCC 的简单定义与计算定义

ios - 在Objective-C中需要声明哪些场景关键字 “volatile”?