C++: Why Use const?

Started by
32 comments, last by snk_kid 19 years, 5 months ago
Ah, right. Thanks.
Advertisement
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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:

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]
Things change.
Quote:Original post by Oluseyi
One additional caveat to using const effectively is pointers. Consider the following:
const int * p = &someIntVariableconst int const * p2 = &someIntVariable
The first allows the modification of the value pointed to, but not the pointer itself. The second allows neither.


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;}
daerid@gmail.com
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?
[s] [/s]
I can see the fnords.
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)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Oluseyi
const int PI = 3.142;


Is that an Alabama pi?
[grin]
Quote:Original post by UfoZ
Quote:Original post by Oluseyi
const int PI = 3.142;


Is that an Alabama pi?
[grin]


_ROFL_, i love typos!
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Quote:Original post by silvermace
_ROFL_, i love typos!


Well, g++ does issue a warning, which is a Good Thing™.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement