[dmd-internals] ref const(T) doesn't accept struct temporaries?

Sean Kelly sean at invisibleduck.org
Mon Dec 13 11:10:28 PST 2010


Yeah, that;s the discussion I found :-)  I saw the other one too, but yours was more detailed.

What gets me about "auto ref" is that the "auto" here really has two purposes.  First, the function according to the questionably useful example:

auto ref T min(T)(auto ref T x, auto ref T y) {
    return x < y ? x : y;
}
int x = 1;
int y = min(x, 1)++;

I say questionably because I still doubt the need for passing the reference to the original value through in this case.  I'm sure there are examples justifying this, but the min() one doesn't cut it :-)

The other purpose is that it provides optimal performance in instances where the function may take both rvalues and lvalues and the function creator doesn't care that lvalues will be passed by ref (or const ref).  I can see "auto ref" and "auto const ref" being used all over the place "just in case," kind of like "register" was in C, which is not at all appealing.  I don't know what the answer is, but something about "auto ref" parameters just feels a bit clunky.

I also agree with Don about the use of "auto" rather than some other keyword, but I can see why it was chosen.

On Dec 13, 2010, at 6:04 AM, Steve Schveighoffer wrote:

> Ran into the same problem long ago when moving dcollections to D2.
> 
> Andrei opened a bug on this: http://d.puremagic.com/issues/show_bug.cgi?id=3659
> 
> Here's my rant on the NG :)
> 
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=109239
> 
> 
> -Steve
> 
> 
> 
> ----- Original Message ----
>> From: Sean Kelly <sean at invisibleduck.org>
>> To: Discuss the internals of DMD <dmd-internals at puremagic.com>
>> Sent: Fri, December 10, 2010 5:15:55 PM
>> Subject: Re: [dmd-internals] ref const(T) doesn't accept struct temporaries?
>> 
>> Um... problem:
>> 
>>    Error: function core.time.Duration.opEquals  type signature should be const 
>> bool(ref const(Duration)) not const bool(Duration  other)
>> 
>> I'm being forced to define a method that doesn't accept  temporaries?  Not a 
>> huge problem since I don't use opEquals very often,  but...
> 
> 
> 
> _______________________________________________
> dmd-internals mailing list
> dmd-internals at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-internals



More information about the dmd-internals mailing list