Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
HOWTO: const and pointer variants in C and C++
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Andrey Tarasevich, post: 3674339"] 'const' in C++ has serves multiple purposes. It can 1) declare constness of an object itself, and 2) declare constness of an access path to an object, 3) serve as a distinguishing trait in overload resolution. How the third role can be used in "optimization" is rather obvious. But these are basically user optimizations, as opposed to complier optimizations. I assume that you are talking about compiler optimizations, to which the third role is not immediately relevant. I don't think I need to provide examples of how the first role of 'const' can speed up the code. They are immediately obvious as well. Constant values can be calculated at compile time. Code that depends on constant values can be reduced at compile time. Etc. The second role of 'const' is more about discipline, than about optimization. In general case, the constness of access path does not imply the constness of the object this access path leads to. Which means that in general case without preforming a thorough aliasing analysis, the compiler can't do much based on the constness of access path itself. And if the compiler is smart enough to perform a sufficiently thorough aliasing analysis, it should be smart enough to optimize it regardless of whether the explicit 'const' is supplied by the user or not. So, I'd agree that the second role of 'const' by itself is not immediately useful for compiler optimizations. However, it is worth noting that it might become quite useful if the user explicitly gives the compiler the permission to perform "overly agressive" optimizations (meaning, that they might be formally invalid in general case) by assuming some aliasing properties of the code, instead of trying to derive them from the code itself. (I'm not sure, but it is quite possible that the combination of 'const' with C99's 'restrict' allows for more valid straightforward optimizations that just a mere 'restrict'.) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
HOWTO: const and pointer variants in C and C++
Top