Re: Const Considerations
Craig wrote:
c) Are there, in your experience, domains or niches where const is more
prevalent (say, math libraries, or string manipulation, etc.)
In my experience, there are no domains which even try to not use
it.
Const usage may be less consistent in fields where the developers are
not trained computer scientists, or precisely where software
development is not their main skill area.
I think even there, it depends on the age of the programmers.
Const correctness is an integral part of C++, and anyone
learning C++ today, even on the side, learns it as such, and the
question as to whether it is even possible to program without it
doesn't occur. But that's today---programmers who learned C or
C++ before about 1995 may not have learned it this way, and as
long as they are not professional programmers, and what they
learned works, they will doubtlessly continue to use what they
learned.
This is true of a number of
scientific groups where the main skill base might be in mathematics,
engineering or a branch of the sciences (I work in an organisation
where this is typical). It is common to find people who have taught
themselves C++ in these settings, but only enough to get them by for
the projects being undertaken. These can sometimes grow into
substantial community projects and the inertia of users makes it
difficult to go back and "constify" the code base (people don't like
having their API's broken!). Other times there is simply not the
perceived need to go back and make const usage more consistent once the
skill levels improve.
The problem with not using const is that you cannot use
temporaries. That's a pretty big problem, I would think, and a
very strong motivation for using const.
Of course, there are still compilers out there which don't
enforce this rule, or even generate a warning, so if the group
is using only these compilers, this motivation to change might
not be there.
For situations such as these, const usage may be inconsistent or fairly
sparse due to a lack of understanding. Perhaps the main reason is that
the code compiles without const
That's just my point, it generally doesn't, at least with a
conformant compiler.
and omitting const doesn't lead to a
run-time bug. Therefore, it is only once the developers learn about why
const is useful that they will start to use it themselves.
Unfortunately, this knowledge is often not one of the earlier things a
self-taught C++ developer learns.
If they're using g++ to learn, they'll soon be bothered by the
fact that they cannot pass a temporary to a non-const reference.
Try the following with your compiler:
struct S {} ;
S
f()
{
return S() ;
}
void
g( S& )
{
}
int
main()
{
g( f() ) ;
return 0 ;
}
It requires a diagnostic, and has since CFront 2.1, at least.
(That was something like 1989.) It won't compile with g++, and
shouldn't with any compiler. (In the case of both Sun CC and
VC++, you have to set the warning level to maximum in order to
get a warning---at least the VC++ warning qualifies it as a
"nonstandard extension". Interestingly enough, in much earlier
versions of Sun CC, 3.0.1, for example, it was a warning that
you couldn't turn off.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]