Andrei Alexandrescu wrote: > 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
I would like to make the suggestion that Rebindable be discussed in the section of the documentation that discusses const and invariant ( http://www.digitalmars.com/d/2.0/const3.html ). It would make the situation clearer. Now it _is_ a part of phobos rather than a part of D itself, so I don't know if you want to discuss it in depth in the Language section of D's documentation, but I think that it would be quite helpful for those learning D. As it is, I don't find it entirely clear from that page that a const reference is const in the sense that both the reference and what it references are const, though arguably it follows from the transivity of const. Personally, I had to play around with some code to figure out in what way a const reference was const. In any case, I thank anyone who's worked on the documentation. While it certainly isn't perfect, I've found it to be a big help. - Jonathan M Davis
