是否还在使用 register 关键字?

2021-12-20 00:00:00 performance caching c++ latency

刚刚在 C++ 中遇到 register 关键字,我想知道这似乎是个好主意(将某些变量保存在寄存器中)编译器是否会默认执行此操作?

Just came across the register keyword in C++ and I wondered as this seems a good idea (keeping certain variables in a register) surely the compiler does this by default?

所以我想知道这个关键字还在使用吗?

So I wondered is this keyword still used?

推荐答案

大多数实现只是忽略 register 关键字(除非它强加了语法或语义错误).

Most implementations just ignore the register keyword (unless it imposes a syntactical or semantical error).

该标准也没有说任何东西都必须保存在寄存器中;只是它暗示了该变量将经常使用的实现.它的使用甚至已被弃用.

The standard also doesn't say that anything must be kept in a register; merely that it's a hint to the implementation that the variable is going to be used very often. Its use is even deprecated.

3) 寄存器说明符是对实现的提示,即如此声明的变量将被大量使用.[ 注意:可以忽略提示,并且在大多数实现中,如果采用变量的地址,它将被忽略.这种用法已被弃用(见 D.2).― 尾注 ]

7.1.1 Storage class specifiers [dcl.stc]

3) A register specifier is a hint to the implementation that the variable 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 variable is taken. This use is deprecated (see D.2). ― end note ]

相关文章