Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for deprecated register keyword C++ 11

Tags:

c++

c++11

I have read (here, for example) that the register keyword is deprecated in C++ 11. As such, is there an equivalent to this storage-class specifier in the newer versions of the standard, or is it taken care by the compiler?

like image 242
Hariprasad Avatar asked Dec 16 '13 18:12

Hariprasad


People also ask

Is register deprecated?

The register keyword was deprecated in the 2011 C++ standard, as its effect was already implicit in the language. It remains reserved for future use by the standard, and is time to remove its vestigial specification.

Is register deprecated in C++?

The register keyword was deprecated in C++, until it became reserved and unused in C++17. It suggests that the compiler stores a declared variable in a CPU register (or some other faster location) instead of in random-access memory.

Is register a keyword in C?

Register variables tell the compiler to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility.


2 Answers

We can find the rationale for deprecating register in defect report 809: Deprecation of the register keyword which says (emphasis mine):

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.

The removal of register for C++17 was approved in the Lenexa meeting but it is still reserved for future use.

The register keyword was deprecated in the 2011 C++ standard, as its effect was already implicit in the language. It remains reserved for future use by the standard, and is time to remove its vestigial specification.

Because of the as-if rule the compiler only has to emulate the observable behavior of the program and therefore the optimizer can via the as-if rule choose to keep a variable in a register if it won't effect observable behavior and presumably will in most cases make better choices since it usually has more information.

For reference also see role of "register" C keyword? from the gcc mailing list, one of the replies in the thread says:

I don't think the "register" keyword ever affected register allocation in gcc. For that you have to go back to compilers of the 1970s.

The register keyword does still have a use, though, in a gcc extension: gcc uses it in combination with asm to implement register variables.

like image 196
Shafik Yaghmour Avatar answered Oct 14 '22 02:10

Shafik Yaghmour


It was never a guarantee that the compiler would listen to you if you used the keyword (and in some cases, it was a guarantee it would ignore you, like if you took the address).

This hinting ability is now deprecated, and there is no replacement (that is standard. inline asm is not standard, but could be used).

like image 37
soandos Avatar answered Oct 14 '22 02:10

soandos