Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...