Why the need for an only const ref?

Janice Caron caron800 at googlemail.com
Mon Dec 3 05:57:46 PST 2007


On Dec 3, 2007 10:35 AM, Walter Bright <newshound1 at digitalmars.com> wrote:
> Janice Caron wrote:
> > On 12/1/07, Derek Parnell <derek at psych.ward> wrote:
> >> What is a D code example that catches inadverant reallocations at compile
> >> time?
> >
> > Just don't do it. Head const is a purely local thing. The designer of
> > a function knows what it's supposed to do, and if they make it do
> > something stupid, it's a bug
>
> That's right. Const-correctness is a thing that becomes necessary when
> crossing API boundaries, it is of only minor utility for local
> references only.

Ooh, ooh - I just figured out - we DO have head constness in D after
all! You want a mutable buffer you can't move? No problem! Here's how
it's done...

    private char[] buffer_ = new char[MAXBUFSIZE];
    char[] buffer() { return buffer_; }
	
    buffer[0] = '+'; /* OK */
    buffer ~= '+'; /* Error */



More information about the Digitalmars-d mailing list