[英]Register storage specifier in C++
I am curious to know to behavior of register
storage specifier in C/C++
. 我很好奇知道
C/C++
的register
存储说明符的行为。 Following program valid in C++
, but in case of C
it's not valid. 以下程序在
C++
有效,但在C
情况下无效。
int main()
{
register int i;
int* b = &i; // Valid in C++ but not in C
}
So, My question I, Why C++ allowed address operator(&) to a register variable? 那么,我的问题我, 为什么C ++允许地址运算符(&)到寄存器变量?
PS : I know register storage specifier is deprecated in modern compiler. PS:我知道寄存器存储说明符在现代编译器中已弃用。
Your answer can be found here: 您的答案可以在这里找到:
http://en.cppreference.com/w/cpp/language/storage_duration http://en.cppreference.com/w/cpp/language/storage_duration
In C, the address of a register variable cannot be taken, but in C++, a variable declared register is semantically indistinguishable from a variable declared without any storage class specifiers.
在C语言中,不能获取寄存器变量的地址,但是在C ++语言中,声明为register的变量与没有任何存储类说明符的声明变量在语义上没有区别。
C and C++ diverged before the C language was standardized. 在C语言标准化之前,C和C ++有所分歧。 Both adopted the
register
keyword but only C added the restriction on the &
operator. 两者都使用了
register
关键字,但是只有C添加了对&
运算符的限制。
Digging into the history, the C++98 standard says, 回顾历史,C ++ 98标准说,
A
register
specifier has the same semantics as anauto
specifier together with a hint to the implementation that the object so declared will be heavily used.register
说明符与auto
说明符具有相同的语义,并带有实现的提示,即声明的对象将大量使用。 [ Note: the hint can be ignored and in most implementations it will be ignored if the address of the object is taken.[ 注意:提示可以忽略,在大多数实现中,如果使用对象的地址,则将忽略提示。 — end note ]
— 尾注 ]
Subsequently it was deprecated, which is an opposite direction from strengthening it with semantic restrictions. 随后不推荐使用,这与通过语义限制进行增强相反。 It is scheduled for removal in the upcoming C++17 standard.
计划在即将发布的C ++ 17标准中将其删除。
( auto
was later repurposed in C++11. Its old meaning was the same as the default storage of a local variable.) (
auto
后来在C ++ 11中被重新利用。它的旧含义与局部变量的默认存储相同。)
So, My question I, Why C++ allowed address operator(&) to a register variable?
那么,我的问题我, 为什么C ++允许地址运算符(&)到寄存器变量?
The answer is: 答案是:
Because C and C++ are different languages and follow different standards as pointed out in the other answers. 因为C和C ++是不同的语言,并且遵循其他答案中指出的不同标准。
The difference is explained by this quote from the C++ Standard (7.1.1 Storage class specifiers) 区别在于C ++标准(7.1.1存储类规范)中的引号
3 A register specifier is a hint to the implementation that the variable so declared will be heavily used.
3寄存器说明是实现的暗示,即声明的变量将被大量使用。 [Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken .
[注意:提示可以忽略, 在大多数实现中,如果采用变量的地址,则将忽略提示。 This use is deprecated (see D.2).
不建议使用此用法(请参阅D.2)。 —end note]
—尾注]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.