From: pedro on
Hi

I've wondered for some time if C++ would benefit if class member
functions were const by default, the opposite of the current
behaviour. So that if a member function could potentially update
mutable state, it would have to explicity override the const default
in the signature.

In reading some of the arguments in favour of functional programming,
I've seen many mentions of the lack of side-effects in such
languages. This has given me added impetus led me to explore the use
of const in C++.

Any thoughts ?

Thanks

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

From: Francis Glassborow on
pedro wrote:
> Hi
>
> I've wondered for some time if C++ would benefit if class member
> functions were const by default, the opposite of the current
> behaviour. So that if a member function could potentially update
> mutable state, it would have to explicity override the const default
> in the signature.
>
> In reading some of the arguments in favour of functional programming,
> I've seen many mentions of the lack of side-effects in such
> languages. This has given me added impetus led me to explore the use
> of const in C++.
>
> Any thoughts ?
>
> Thanks
>

But some of us think that all declarations of objects should be constant
by default but we realise that it is far, far too late to spend time
arguing about it. Any change would break far too many millions of lines
of existing code.

So my thought is that you should move on and spend effort on things that
can be done in practice.

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

From: Jeff Schwab on
pedro wrote:

> I've wondered for some time if C++ would benefit if class member
> functions were const by default, the opposite of the current
> behaviour. So that if a member function could potentially update
> mutable state, it would have to explicity override the const default
> in the signature.
>
> In reading some of the arguments in favour of functional programming,
> I've seen many mentions of the lack of side-effects in such
> languages. This has given me added impetus led me to explore the use
> of const in C++.
>
> Any thoughts ?

const probably ought to be the default for both functions and variables.
I make everything const, unless there is specifically a reason to do
otherwise.

C++ can actually be a very nice language for FP; my only suggestions
(aside from the point you already made about immutability) are that you
(1) use the "collect, select..." family of names, rather than "map,
filter..." (to avoid compiler errors about ambiguity with std::map), (2)
pass function arguments first, as in select(somefunc, somecollection),
and (3) don't worry too much about the run-time overhead of
return-by-value. Modern compilers implement NRVO, and you can always
bind temporary return values to const& in the calling scope. You may
also want to read the online documentation for Boost.Phoenix, primarily
for ideas.

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

From: Daniel Krügler on
On 30 Jan., 09:29, pedro <pedroke...(a)gmail.com> wrote:
> I've wondered for some time if C++ would benefit if class member
> functions were const by default, the opposite of the current
> behaviour. So that if a member function could potentially update
> mutable state, it would have to explicity override the const default
> in the signature.
>
> In reading some of the arguments in favour of functional programming,
> I've seen many mentions of the lack of side-effects in such
> languages. This has given me added impetus led me to explore the use
> of const in C++.
>
> Any thoughts ?

Well, you are a wise (wo)man, if you don't start to
change defaults of anything that is well established
and exists for a long time ;-)

One the other hand, there is no reason to keep the same
tradition, if you provide new features. In this regard,
you can consider that the upcoming C++ lambda types
follows the const-by-default principle, i.e. you have
to mark the lambda-declarator with the keyword mutable
you explicitly allow non-const to be in effect.

HTH & Greetings from Bremen,

Daniel Kr�gler


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

From: Christian Freund on
{ quoted banner removed. please avoid top-posting. -mod }


I guess it is not millions, it is much (much) more.
Maybe you better convince/pay your compiler-creator to get it in, with some
proprietary compiler-flag.

"Francis Glassborow" <francis.glassborow(a)btinternet.com> schrieb im
Newsbeitrag news:cuWdnRz3i9wgbP7WnZ2dnUVZ7oBi4p2d(a)bt.com...
> pedro wrote:
>> Hi
>>
>> I've wondered for some time if C++ would benefit if class member
>> functions were const by default, the opposite of the current
>> behaviour. So that if a member function could potentially update
>> mutable state, it would have to explicity override the const default
>> in the signature.
>>
>> In reading some of the arguments in favour of functional programming,
>> I've seen many mentions of the lack of side-effects in such
>> languages. This has given me added impetus led me to explore the use
>> of const in C++.
>>
>> Any thoughts ?
>>
>> Thanks
>>
>
> But some of us think that all declarations of objects should be constant
> by default but we realise that it is far, far too late to spend time
> arguing about it. Any change would break far too many millions of lines
> of existing code.
>
> So my thought is that you should move on and spend effort on things that
> can be done in practice.


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