Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with register keyword in C++? [duplicate]

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?

like image 492
Destructor Avatar asked Dec 25 '22 19:12

Destructor


1 Answers

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.

like image 113
Angew is no longer proud of SO Avatar answered Jan 12 '23 04:01

Angew is no longer proud of SO