Getting the const-correctness of Object sorted once and for all

Steven Schveighoffer schveiguy at yahoo.com
Mon May 14 12:24:15 PDT 2012


On Mon, 14 May 2012 13:08:06 -0400, Tove <tove at fransson.se> wrote:

> On Monday, 14 May 2012 at 16:53:24 UTC, Timon Gehr wrote:
>> On 05/14/2012 06:10 AM, Chris Cain wrote:
>>> On Monday, 14 May 2012 at 02:57:57 UTC, Mehrdad wrote:
>>>> The problem is that it's unavoidable.
>>>>
>>>> i.e. you can't say "don't mark it as const if it isn't const",
>>>> because, practically speaking, it's being forced onto the programmers
>>>> by the language.
>>>
>>> You're really against const in this language, huh?
>>>
>>
>> I guess this is not the most important point.
>> He has been trying to use const like in OO-ish C++.
>> This just does not work, because D const is detrimental to OO
>> principles when used that way.
>> The proposal is about _enforcing_ C++-like usage of const.
>
> but c++ has the 'mutable' keyword as an easy escape route... which saved  
> me a bunch of times... guess one can emulate it with a library-solution  
> using nested classes? But... what about structs?
>
> class Outer
> {
>    int i = 6; // mutable
>
>    class Inner {
>      int y=0;
>
>      int foo() const
>      {
>        // ++y; // fail
>        return ++i; // look ma, mutable const
>      }
>    }
>    Inner inner;
>    this()
>    {
>      inner = new Inner;
>    }
>    alias inner this;
> }

I have never seen this suggested before.  I would guess that it might be
considered a bug, since you are accessing the outer instance via a pointer
contained in the inner instance.

But I like the approach (even if it is a bug)!

-Steve


More information about the Digitalmars-d mailing list