Is the register keyword still used?

前端 未结 6 1270
耶瑟儿~
耶瑟儿~ 2020-12-03 11:54

Just came across the register keyword in C++ and I wondered as this seems a good idea (keeping certain variables in a register) surely the compiler does this by default?

6条回答
  •  余生分开走
    2020-12-03 12:26

    Only specific number of registers are available for any C++ program.

    Also, it is just a suggestion for the compiler mostly compilers can do this optimization themselves so there is not really much use of using register keyword and so more because compilers may or may not follow the suggestion.

    So the only thing register keyword does with modern compilers is prevent you from using & to take the address of the variable.

    Using the register keyword just prevents you from taking the address of the variable in C, while in C++ taking the address of the variable just makes the compiler ignore the register keyword.

    Bottomline is, Just don't use it!

    Nicely explained by Herb:
    Keywords That Aren't (or, Comments by Another Name)

提交回复
热议问题