Why the need for an only const ref?

Janice Caron caron800 at googlemail.com
Mon Dec 3 07:45:49 PST 2007


On Dec 3, 2007 3:05 PM, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
> I think his point is that private means nothing *within* a module, only
> between modules.  So if you write your program as one big module with
> that trick then it's basically like you didn't use it at all.

Hmm. But it will work even /without/ the word private. That's really
just there for documentation.

As I understood it, the goal was to catch reallocation bugs at compile
time, so, say you have a function that looks like this:

    void f(char[] buffer)
    {
        /*...*/
    }

you can just change it to

    void f(char[] buffer_)
    {
        char[] buffer() { return buffer_; }
        /*...*/
    }

and immediately you get compile-time checking. It's not whether or not
you make the original variable private or not that matters, it's just
the fact that you must always refer to it by its function-call-name.
That's why I added the underscore to the "real" variable, but if
that's not a strong enough deterent then make it

    void f(char[] WARNING_doNotUseThisBuffer)
    {
        char[] buffer() { return WARNING_doNotUseThisBuffer; }
        /*...*/
    }

(but personally I think the trailing underscore is enough). There's no
need for any of this to "spread" to the outside world, because
head-constness is only local anyway.



More information about the Digitalmars-d mailing list