4 ms·
const type* const has always been a warning flag to me when I read it in other people's code. What are the advantages of making the pointer itself const?
by duncanj 16y ago
const type* const has always been a warning flag to me when I read it in other people's code. What are the advantages of making the pointer itself const?
- slug 16y agoI find it the opposite, when I read code that doesn't use const at all. In that case you want to make sure that code doesn't change either the value the pointer is pointing to and where the pointer is pointing to :) <pre> #include <stdio.h> void function ( int const * const value ) { printf ("%d\n", * value); / * more code * / ( * value)++; / * more code * / ++value; } int main(int argc, char * * argv) { int value = 1; int * pvalue = &value; function(pvalue); return 0; } $ gcc t1.c t1.c: In function ‘function’: t1.c:5: error: increment of read-only location ‘* value’ t1.c:7: error: increment of read-only location ‘value’ </pre> quick googling: http://www.cprogramming.com/tutorial/const_correctness.html http://www.cprogramming.com/tutorial/const_correctness.html EDIT: how can we properly paste code in here? The * mess things up.
- parenthesis 16y agohttp://news.ycombinator.com/formatdoc http://news.ycombinator.com/formatdoc
- pietrofmaggi 16y agoUsually you take a look at the FAQ and read the http://news.ycombinator.com/formatdoc http://news.ycombinator.com/formatdoc page. #include <stdio.h> void function(int const * const value) { printf ("%d\n", *value); /* more code */ (*value)++; /* more code */ ++value; } int main(int argc, char **argv) { int value = 1; int *pvalue = &value; function(pvalue); return 0; } $gcc t1.c t1.c: In function ‘function’: t1.c:9: error: increment of read-only location t1.c:13: error: increment of read-only location BTW: I agree, const is really useful EDIT: I've recompiled on OSX to correct the error message with the correct lines.
- duncanj 16y agoI think you misunderstand. When I see, in a function like yours void function(int const * const value) it worries me because it shows me that the writer doesn't know the meaning of const. There is no added value of this declaration over void function(int const *value) or the more commonly seen void function(const int *value) The only thing that has changed in your code is that the implementor of the function can't modify his own private copy of the pointer.
- flatline 16y agoTo ensure there are no side effects - at least to ensure that that object in question is not mutated. I think const refs are a little more useful than const pointers for this.
- alextgordon 16y agoDo you find const actually catches bugs? I admittedly don't use C++ that frequently, but if I make my code const-correct it always seems to cause a lot of false positives, while seldom finding actual bugs.
- danieldk 16y agoI disagree, especially const-correct methods help avoiding mutation. The primary mistake IMO is that const can be cast away. Invariant should be invariant (like D).
- jpr 16y agoA lot of people seem to be horrified that you can cast constness away. Maybe they have more traumatic experiences than I do, but I believe that if something is possible to do (modifiying an object that is declared const), it should be possible in the language as well. That does not mean it should have well specified semantics or that it should be done without proper considerations.
- nickelplate 16y agoconst does not so much catch bugs as it prevents them. It is a way to specify a contract. If your code is const-correct, the next programmer using or maintaining your code can read a function or method signature and know immediately which parameters will not change, or whether the object will be modified. Effective C++ explains this very well, and I fell in love with const after reading its second edition almost 10 years ago.
- nostrademons 16y agoThat would be const type pointer. (HN apparently won't let me type more than one asterisk in the message without turning everything between them to italics.) const type pointer const is a const pointer to a const object, i.e. the pointer itself can't be mutated to point to a different object. I've never found a need for it. I usually don't care if the pointer itself is modified, particularly since such modifications are usually local and obvious.