What guarantees does D 'const' provide, compared to C++?

Chris Cain clcain at uncg.edu
Thu Aug 16 18:00:34 PDT 2012


On Friday, 17 August 2012 at 00:44:20 UTC, Chris Cain wrote:
> Also, D's const is _not_ a guarantee that there are no mutable 
> references to something. That'd be immutable.

And, by the way, I'd call this a bug (not sure if reported yet):



int yourGlobalCounter;

struct S
{
     int*[] items;

     this(int i)
     {
         items = new int*[1];
         items[0] = &yourGlobalCounter;
         yourGlobalCounter = i;
     }

     ref const(int*[]) getItems() const
     {
         ++yourGlobalCounter;
         return items;
     }
}

import std.stdio;

void main() {
     immutable(S) s = immutable(S)(0);
     auto it = s.getItems();
     writeln(*it[0]);
     s.getItems();
     writeln(*it[0]);
}




More information about the Digitalmars-d mailing list