关于C和C++的register
关键字
近日看到一些C++代码在for
循环里加入了register
关键字来试图声明寄存器变量,并声称可以优化速度,那么这是不是有效的呢?我们将看到,这个写法在C语言里也许(但未必)是有效的,但是在C++语言里可能不是非常有效了。
In C++
博主在写这篇博客的时候最新C++标准是ISO/IEC 14882:2017,C++20目前只出了一个working draft,还没有出台出现正式的文件,因此我们以C++17为最新标准。
我们翻开C++17标准,里面有类似的叙述:
第C.1.6款:
Change: In C++,
register
is not a storage class specifier.
Rationale: The storage class specifier had no effect in C++.
第C.4.3款:
Change: Removal of
register
storage-class-specifier.
Rationale: Enable repurposing of deprecated keyword in future revisions of this International Standa