From: Goran on
On Mar 29, 2:34 am, Walter Bright <newshou...(a)digitalmars.com> wrote:
> Peter C. Chapin wrote:
> > void f(const int x)
> > {
> > const int y = 2*x + 1;
> > // Here x and y are local constants.
> > }
>
> > But... is my code better for this?
>
> I think so, it's a style I've been slowly adopting.

I am not doing this, I too think it's better than without a const.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Öö Tiib on
On 31 m�rts, 04:32, Seungbeom Kim <musip...(a)bawi.org> wrote:
> And it is not the specific function (read) but the style that I was
> talking about. I don't remember seeing such a style in The C Programming
> Language, The C++ Programming Language, or GNU coreutils, etc.
> For example, I haven't seen anything like below:
>
> int main(const int argc, char** const argv) { ... }
>
> void sighandler(const int signum) { ... }
>
> reference operator[](const size_type n) { /* return p[n]; */ }

Guarding impact of const is naturally smaller there. It is
implementation detail of temporary local value.

Problems are that: Top level const confuses people who did not know or
forgot that it does not modify functions signature. For array
parameters lot of policies demand that [] notation instead. If to put
formal parameter list aside then pointer constant has next to non-
existing technical difference from a reference. Usage syntax however
is very different. That is also hard to explain to people who are not
accustomed to semantic anarchy of C++.

So ... const is important in interface where it promises not to modify
something and is very valuable for data that is read from multiple
threads. Locally however it is implementation detail that guards
something local from a typo few lines apart. Such typos happen but
they usually come out with quite subtle unit testing. Therefore I
would leave such implementation details up to implementer to decide.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]