5

I was reading this and it says that the register keyword will most probably be removed from the next C++ standard. It also says that register was deprecated in 2011. So, what's wrong with register storage class specifier?

I think modern compilers are very smart and they implicitly optimize frequently used variables for speed (fast access) and puts them in CPU registers.

However, C++ experts also say don't or never use register. As such, what's the problem with the register keyword?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 7
    I think you already answered: it was only a recommendation for compiler and compiler better can optimize the storage of a variable. – m0nhawk May 23 '15 at 16:53
  • 3
    It is a useless and sometimes misleading hint. (Kind of like how `inline` no longer means the function will be inline, only that it can be defined multiple times safely). – nneonneo May 23 '15 at 16:58
  • Consider the other side: what if an implementation actually put every `register` variable in a register? How could that be handled? Some answers [here](https://stackoverflow.com/questions/28928674/can-an-implementation-consider-hints-as-actual-statements). – edmz May 23 '15 at 17:05

2 Answers2

10

You've pretty much answered your own question:

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

That's precisely the point—optimisers are so good at register allocation nowadays that any attempt from the programmer to enforce their will through the register keyword would likely lead to a pessimisatin, and is therefore simply ignored by the compiler. Remember that register was never a binding requirement, always just a hint to the compiler. Now that they rightfully scoff at such hints, the keyword is simply obsolete and useless.

So, to directly answer your question of "what's wrong with it:" it no longer serves any purpose whatsoever, as the only one it ever had ("hint to the compiler to put this thing in a register") is now superseded by the compilers being way better at this than humans.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

Standard doesn't require that register variable to be put into registers, instead it's just a hint for compiler for variables that are often used. And compiler can determine it on its own.

Here, clause regarding register keyword from the link you posted:

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 ]

myaut
  • 11,174
  • 2
  • 30
  • 62