Fun with C++: Useless Consts?

Many C++ programmers see the keyword “const” as something redundant that only gets in the way. They curse Stroustrup and his ancestors each time they encounter a const-related compile error because of that view and fail to use it correctly quite often (note that I’m not saying that I’m not one of these people, but I curse neither Stroustrup nor anyone else.)

If you ask them about the purpose of the const keyword, they would give you the classic answer that any C++ textbook gives: variables declared as consts cannot be changed by the programmer, and const class methods cannot modify their instance data (and can be called on const class instances), etc.

The programmer may give you the textbook answer (or some mutilated version of it, depending on which textbook she read and how much of it she actually remembers,) but she is wondering with herself who will stop the other programmer to change the header file and remove the const from the declaration? Who is there to prevent her from doing so?!

She may even make a conjecture that if she removes all the consts from all her source files and headers (including standard and 3rd party headers she uses,) then her programs will continue to compile and work just fine (after all, the const keyword has no runtime effect. It’s an exclusively compile-time construct, is it not? So if her programs used to work, and they compile after removing all consts, they should continue to run without problem.)

If she is a little bit more sophisticated and skilled in C++, she knows that the C++ compilers differentiates between functions that take const arguments and those that take exactly the same arguments, but not const. This fact lets you have function overloads that only differ in constness of their arguments. The same is also true for template specializations (which is a form of overload anyway.)

So this more sophisticated C++ programmer will conjecture that if she removes all the consts and eliminates the redundant overloads (which were nothing but nuisances to begin with) then her programs will compile.

Now, is she right? Is this the case with const?
(I should say that I know of one case that is not so (the program will not compile without the the const) but it’s not a common case. I’ll write about that in a later post (if no one guesses it first.) I’m genuinely interested in other such cases.)
(Another note. The use of const frees the compiler to do some optimizations that may not be safe with a non-const object. My question is about correctness and not about performance.)

VN:F [1.9.13_1145]
Rating: 8.2/10 (5 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
Fun with C++: Useless Consts?, 8.2 out of 10 based on 5 ratings

2 Comments

  1. MatGill says:

    Just a quick note. First of all I’m probably too old and too much separated from the programming world that I can’t possibly guess the answer to your final questions (although I think it has something to do with operators). Anyhow, my point is that you cannot easily get rid of overloaded functions. Normally overloaded const functions (in any form), are expected to have the same running semantic. But this is not a rule in the language itself. Careless programmers might overload with different semantics. There might be even cases when such thing *is* justified.

    We are far from any ideal world, you know.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  2. yzt says:

    Absolutely agreed. I was actually leaving the removal of spurious overloads as an exercise for the reader. ;-) (Meaning, I couldn’t say how to do it!)
    As you say, it can even be justified. (For example, think about a LargePagedArray class that pages (or swaps?) it’s elements to a disk file. The operator [] of such class has to behave differently in const and non-constant mode. In the latter case, the operator first has to write the element (or group of elements) that are being accessed to disk if they are “dirty”. (This was the simplest example that occurred to me that was logical!))

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply