Best practices for logical const

Stanislav Blinov stanislav.blinov at gmail.com
Sat Feb 15 19:25:08 PST 2014


On Saturday, 15 February 2014 at 04:03:51 UTC, Adam D. Ruppe 
wrote:

What about a library solution for something like C++-esque 
mutable?

struct Mutable(T)
{
     private T val;

     this(T v) { val = v; }

     @property ref T get() const { return *cast(T*)&val; }
     alias get this;
}

import std.stdio;

struct Foo
{
     private Mutable!int cache;

     void bar() const
     {
         writeln("Updating cache");
         ++cache;
     }

     @property int cached() const { return cache; }
}

void main()
{
     Foo foo;
     writeln(foo.cached);
     foo.bar();
     writeln(foo.cached);
}


More information about the Digitalmars-d mailing list