|
|
| const correctness and C++ |
|
Thomas Fjellstrom
Member #476
June 2000
|
Yes or No? Discuss! -- |
|
gnolam
Member #2,030
March 2002
|
Five tons of flax. -- |
|
Kibiz0r
Member #6,203
September 2005
|
Ah, I miss const correctness. It was sort of like a little puzzle to play with while you wrote tedious low-level code. --- |
|
Thomas Fjellstrom
Member #476
June 2000
|
Ah. I was kind of hoping for some enlightened discussion of the topic. -- |
|
Arthur Kalliokoski
Second in Command
February 2005
|
It helps you out by spitting out errors when you get confused, what else is there to say? They all watch too much MSNBC... they get ideas. |
|
X-G
Member #856
December 2000
|
Useful for what it is: something to help keep you from screwing up. Obviously due to aliasing rules it has no real functional significance, but you know, the same can be said for a lot of syntactic stuff we use. It helps you, the programmer, write code that doesn't change variables behind the backs of other functions. -- |
|
Oscar Giner
Member #2,207
April 2002
|
I think it makes the code somewhat more readable for other people, if used propertly (ie, don't bypassing it It would be more useful if it couldn't be bypased, and that method couldn't modify global variables or call global/static functions since that would allow compilers to do some optimizations. -- |
|
axilmar
Member #1,204
April 2001
|
Const correctness is misleading, to say the least. It's entirely possible to think that an object is const, whereas some local context uses it as non-const. What is better is pureness (the whole context being const). |
|
cheatscanner
Member #11,712
February 2010
|
i. hate. constants. They are a jerk to have to deal with, especially when a functions asks for a constant and i need to use a non-constant (which doesn't happen often, honestly) but I wish that constants didnt exist, and they were just regular variables. and dislike static too. But I only hate static because I have yet to figure out how to deal with them in cases other than them being functions. >.< just wanted to say that. I know that pro-programmers must use them all the time for time saving techniques and have no problem, I'm sure. But am I pro? No. puts on flame retardant suit |
|
bamccaig
Member #7,536
July 2006
|
I like const-correctness. I certainly use it in C++ (and C?). Whether it's truly useful or not, I can't say. I think it is, but I could be wrong. I wish a similar, though updated, concept existed in C#. I think in modern, managed languages, the compiler should disallow casting constness away. That way, you could actually rely on the concept. If you declare a variable as const (or cast to const) then you could rely on it not being modified no matter what[1]. That would be kind of nice, I think. References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
X-G
Member #856
December 2000
|
Truly immutable types are possible and desirable in general (several languages have them), but it doesn't work for practical reasons in C or C++ due to aliasing. In C/C++, constness is not really about the type itself, but about the function acting upon an instance of a type. When you declare a function as taking a const something, you're saying "I promise not to modify this variable in any way". It's a kind of primitive contract rather than a type feature. You can still violate that contract if you really want to, but you have to actually explicitly say when you do by using a const cast, making accidental changing of objects much less likely. -- |
|
Kibiz0r
Member #6,203
September 2005
|
bamccaig said: I wish a similar, though updated, concept existed in C#. You can get some measure of constness in .NET by marking fields as readonly. The convention is for structs to be completely readonly, so if you follow that guideline then you've got a set of immutable types. Quote: the const modifier ... is used in at least three ways: (a) to indicate that an object is read-only, (b) to indicate that a parameter is being passed by reference only for the sake of efficiency and not because it will be modified as a side-effect of the method call, and (c) to indicate that a class method does not, as a side-effect of its invocation, modify any of the non-static class members. So we can accomplish A, but only for fields of classes or structs. B and C, I think, are becoming less of a problem as side-effects make code harder to test. I find myself writing code in a more functional style when I'm doing TDD. --- |
|
ImLeftFooted
Member #3,935
October 2003
|
I understand there are situations where const allows for extra optimizations. |
|
Goalie Ca
Member #2,579
July 2002
|
in haskell everything is a const. ------------- |
|
verthex
Member #11,340
September 2009
|
I've just noticed that in C++ its used a lot whenever a parameter is passed to a method, especially when working with overloading operators. I never work with the latter and rarely use const but if you don't want a method to change the object being passed then const is the best option, besides saving a copy before passing the object. Very useful indeed, I wonder if Java has a similar problem, most likely not but feel free to let me know.
|
|
|