Jonathan M Davis wrote:
Okay, I'm wondering if there is any way in D to have a reference to
const.  As far as I can tell, if you use const on a reference, you
get a constant reference to constant data.

const A a1; const A a2 = new A(); A otherA = new A(); const A a3 =
otherA;

So, a1 points permanently to null.  You can't change what it points
to (and of course you can't change null).  a2 points to an A which
was constructed on assignment.  a2 can't be made to point to anything
else and you can't change the value that it points to.  a3 points to
another the same A as otherA does, but it can't be made to point to a
different A and it can't alter the value that it points to (while
otherA can be altered to point to a different A and you can use it to
alter the value that it points to).

What I'd _like_ to be able to do is have reference which I can alter
such that it points to a different object but which does not allow me
to alter what it points to.  In C++, you have

const A* a1;  //variable pointer to a constant object. A* const a2;
//constant pointer to a variable object const A* const a3;
//constant pointer to a constant object

From what I can tell, you can do 2 of those in D with pointers:

const (A)* a1;  //variable pointer to a constant object. const (A*)
a3;  //constant pointer to a constant object

But I don't see how to get either a const pointer, so it lacks one of
the three with pointers.  With references, however, it lacks 2 of the
three from what I can tell.  It only has constant references to
constant data.

I'm hoping that I just don't understand well enough how const works
in D, but right now, I don't see any way to get a variable reference
to constant data.  The missing constant pointer to variable data and
constant reference to variable data might be nice, but they don't
matter anywhere near as much to me.

Is there a way to have variable reference to constant data in D?  I
just don't see it at the moment and it would be _really_ useful.

http://www.digitalmars.com/d/2.0/phobos/std_typecons.html#Rebindable

Andrei

Reply via email to