Destructors, const structs, and opEquals

Bruno Medeiros brunodomedeiros+spam at com.gmail
Tue Dec 21 10:46:52 PST 2010


On 10/12/2010 20:58, Andrei Alexandrescu wrote:
> On 12/10/10 12:46 PM, Steven Schveighoffer wrote:
>> On Mon, 06 Dec 2010 08:34:20 -0500, Steven Schveighoffer
>> <schveiguy at yahoo.com> wrote:
>>
>>> On Sun, 05 Dec 2010 09:18:13 -0500, Andrei Alexandrescu
>>> <SeeWebsiteForEmail at erdani.org> wrote:
>
> Sorry, indeed I haven't seen it.
>
> The problem with binding rvalues to const ref is that once that is in
> place you have no way to distinguish an rvalue from a const ref on the
> callee site. If you do want to distinguish, you must rely on complicated
> conversion priorities. For example, consider:
>
> void foo(ref const Widget);
> void foo(Widget);
>
> You'd sometimes want to do that because you want to exploit an rvalue by
> e.g. moving its state instead of copying it. However, if rvalues become
> convertible to ref const, then they are motivated to go either way. A
> rule could be put in place that gives priority to the second
> declaration. However, things quickly get complicated in the presence of
> other applicable rules, multiple parameters etc. Essentially it was
> impossible for C++ to go this way and that's how rvalue references were
> born.
>
> For D I want to avoid all that aggravation and have a simple rule:
> rvalues don't bind to references to const. If you don't care, use auto
> ref. This is a simple rule that works promisingly well in various
> forwarding scenarios.
>
>
> Andrei
>

For some cases, one could just use a differently-named function, instead 
of overloading it (like 'foo'). That wouldn't solve the issue for 
generic code (or operator overloading), yes.

But I wonder if this is common and important enough (versus the 
alternative) to merit a new type qualifier or "storage class".


If this is really necessary, I think it might be better to have rvalues 
convertible to ref const, and instead of "auto ref" have a new qualifier 
that binds *only* to rvalues, like ref(auto) or ref(in) or whatever. 
This way the more common case is simpler (just "const Widget" instead of 
"auto ref const Widget", in the case of opEquals for example). And as 
for distinguishing rvalues, the example above would be:
   void foo(ref(in) Widget); // exploit rvalue, move state
   void foo(Widget);
which also has the advantage of being able to distinguish the rvalue 
without making the whole parameter const (which is unnecessarily 
transitive, unlike C++)

-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list