c++c++11

Replacement for deprecated register keyword 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?


Solution

  • 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.