C++ 中的 register 关键字有什么问题?

What's wrong with register keyword in C++?

我正在阅读 this,它说 register 关键字很可能会从下一个 C++ 标准中删除。它还说 register 已于 2011 年弃用。那么,register 存储 class 说明符有什么问题?

我认为现代编译器非常聪明,它们会隐式优化常用变量以提高速度(快速访问)并将它们放入 CPU 寄存器中。

然而,C++ 专家也表示不要或永远不要使用 register。因此,register 关键字有什么问题?

您几乎已经回答了您自己的问题:

I think modern compilers are very smart so they implicitly optimizes frequently used variables for speed (fast access) & puts them in CPU register.

这正是重点——如今优化器非常擅长寄存器分配,以至于程序员通过 register 关键字强制执行其意志的任何尝试都可能导致悲观情绪,因此被简单地忽略编译器。请记住 register 从来都不是绑定要求,它始终只是对编译器的提示。既然他们理所当然地嘲笑这样的暗示,关键字就已经过时且无用了。

因此,直接回答您的问题 "what's wrong with it:" 它不再有任何用途,因为它曾经拥有的唯一一个 ("hint to the compiler to put this thing in a register") 现在已被编译器取代 方式在这方面比人类更好。

标准不要求将 register 变量放入寄存器,而只是对编译器经常使用的变量 的提示。编译器可以自行确定。

此处,关于您发布的 link 中的 register 关键字的子句:

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 ]