Why the need for an only const ref?

Christopher Wright dhasenan at gmail.com
Fri Nov 30 16:16:42 PST 2007


Jesse Phillips wrote:
> I've been following the const stuff to learn about it and start thinking 
> of applications for it. I've made a post about a suggested syntax for 
> declaring different parts const, but I'm starting to wander why a 
> constant reference with mutable data is needed. Currently const is 
> transitive so a const ref would make const data. I do not see a need for 
> syntax change if we don't need this.
> 
> What is the need for a X const x which lets you change the object in x 
> but not reassign it to another X?

It's helpful when you have complex functions that don't want to rebind 
their arguments but need to modify them, for one.

A common example is a shared buffer: you don't want some class replacing 
the buffer, since you want everyone using the same one all the time, and 
you might copy the reference for ease of use. But you want to modify its 
contents.



More information about the Digitalmars-d mailing list