C++: Why Use const?
Well, here's a few expert opinions...
the first from sutter on const-correctness and optimization. However, you should beware of typedefs and const as it can have unexpected consequences. Lets also not forget about object lifetimes while we're at it. Finally, and somewhat related, you should really preffer iterator over const_iterator.
the first from sutter on const-correctness and optimization. However, you should beware of typedefs and const as it can have unexpected consequences. Lets also not forget about object lifetimes while we're at it. Finally, and somewhat related, you should really preffer iterator over const_iterator.
Edit: Washu beat me to it.
Herb Sutter (whom I refer to as "the man" when it comes to C++) sums up the majority of const related issues in his Guru of the Week article #81
The excellent summary at the end:
Herb Sutter (whom I refer to as "the man" when it comes to C++) sums up the majority of const related issues in his Guru of the Week article #81
The excellent summary at the end:
Quote:From Herb Sutters article:
It's a common belief that const-correctness helps compilers generate tighter code. Const is indeed a Good Thing, but the point of this issue of GotW is that const is mainly for humans, rather than for compilers and optimizers. When it comes to writing safe code, const is a great tool that lets programmers write safer code with compiler checking and enforcement. Even when it comes to optimization, const is still principally useful as a tool that lets human class designers better implement handcrafted optimizations, and less so as a tag for omniscient compilers to automatically generate better code.
Me dan una sacudida eléctrica este hilo de rosca he continuado mientras tiene. También, Akira es un crappy, aunque obra clásica, anime.
...[google]
...[google]
Quote:Original post by Oluseyi
One additional caveat to using const effectively is pointers. Consider the following:The first allows the modification of the value pointed to, but not the pointer itself. The second allows neither.const int * p = &someIntVariableconst int const * p2 = &someIntVariable
Oh happy day when I get to correct Oluseyi!!!
The above code should be:
const int * p = &someIntVariableconst int * const p2 = &someIntVariable
If you want the pointer itself to be const, rather than the object pointed to, you need to put const after the *.
Compile the following code in any decent C++ compiler, and the errors will explain what I mean:
class A{public: A() : x(0) {} int x;};int main(int argc,char* argv[]){ A array[2]; A* p = array; p->x++; // <- Allowed p++; // <- also allowed const A* cp1 = array; A const* cp2 = array; cp1->x++; // <- Disallowed, Object is const cp1++; // <- Allowed, pointer is mutable cp2->x++; // <- Disallowed, Object is const cp2++; // <- Allowed, pointer is mutable A* const pc1 = array; const A* const pc2 = array; pc1->x++; // <- Allowed, Object is mutable pc1++; // <- Disallowed, pointer is const pc2->x++; // <- Disallowed, Object is const pc2++; // <- Disallowed, pointer is const return 0;}
I read those sites, and one of the things I noticed is that if you have a globally optimizing compiler it negates most of the optimization benifits of using const. Since I'm using MSVC++ 7.1 I have that feature. Still I can see it's uses.
Also, if I have a function:
const T f(){return T();}
Will the compiler skip over T's copy constructor and basically turn const T f() into T& const f(), but without the issue of returning a temporary?
Also, if I have a function:
const T f(){return T();}
Will the compiler skip over T's copy constructor and basically turn const T f() into T& const f(), but without the issue of returning a temporary?
Quote:Original post by DudeMiester
Also, if I have a function:
const T f(){return T();}
Will the compiler skip over T's copy constructor and basically turn const T f() into T& const f(), but without the issue of returning a temporary?
That's RVO. Which I trust most compilers worth their salt can do.
I actually have some code that does rely on it for correctness... (with a workaround if it's not available)
Quote:Original post by UfoZQuote:Original post by Oluseyi
const int PI = 3.142;
Is that an Alabama pi?
[grin]
_ROFL_, i love typos!
Quote:Original post by silvermace
_ROFL_, i love typos!
Well, g++ does issue a warning, which is a Good Thing™.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement