Const Ideas (and reference types)

BC NOTmi_emayl_adrez at hotmail.com.remove.not
Sun Dec 2 14:52:22 PST 2007


On Fri, 30 Nov 2007 20:44:33 -0000, Bill Baxter  
<dnewsgroup at billbaxter.com> wrote:

> Janice Caron wrote:
>> Well, I'm still gunning for
>>      const (X)& x;
>>  for mutable refs to const classes. Since I come from a C++ background,
>> & means "reference of" to me, and this reads straightforwardly as "x
>> is a reference to const X".
>>  Of course, x would be a reference even /without/ the ampersand - such
>> is the nature of classes in D. But writing it explicitly allows one to
>> put it outside the brackets.
>
> I like where this is going, but my guess is that when/if Walter ever  
> introduces reference types, the syntax for reference-to-T will be "ref  
> T" like the parameter signature rather than C++'s  "T&".
>
> So if you're going peel off the hidden ref, I think you might should  
> make it:
>
>      ref X x --> const ref(T) x;
> or          --> const (T)ref x;
> or          --> ref const(T) x;
>
> #3 seems pretty good to me.  But anyway no one knows what Walter will  
> decide to do.

#2 is most similar to const(T)[] x and const(T)* x etc.

> About references generally, one big difference between C++ references  
> and D classes is that you can't reassign a C++ reference.
>       // C++
>       int& x = y;
>       x = z;  /// error
>
> In that sense, the C++ references themselves are always 'const' (aka  
> head-const / final).
> I seem to remember some example in Stroustrup that explained why this  
> behavior was important.  But anyway, D's class references aren't like  
> that, and it makes me wonder if more general D reference types in D  
> would be like that.
>
> --bb

imo c++ should have had &r = &x to rebind r to x.
maybe d could do 'ref r = x' or 'ref r = ref x'



More information about the Digitalmars-d mailing list