structs are now lvalues - what is with "auto ref"?

monarch_dodra monarchdodra at gmail.com
Mon Dec 24 12:42:09 PST 2012


On Monday, 24 December 2012 at 19:09:35 UTC, Jonathan M Davis 
wrote:
> On Monday, December 24, 2012 20:03:24 monarch_dodra wrote:
>> Hum... Indeed, it takes by ref *and* returns by ref. Passing in
>> to out a const ref is *the* smoking gun. Working with D has
>> gotten me used to functions that take by value and return by
>> value...
>> 
>> Snap. You got me.
>
> It wouldn't matter if the function accepted its argument by 
> value...
> 
> - Jonathan M Davis

well, if the function took by value, and returned by ref, then it 
would be just plain *wrong*, no conditions, so that did not even 
cross my mind.

I *thought* min took by ref, and returned by value, hence my 
confusion to the example. I'm legitimatly surprised to learn that 
it works that way, specifically because I've learned to NEVER 
return by ref an input argument exactly because of this...

Side question, is this legal?

"int a = min(min(1,2), min(3,4));"

How long do those temps last? I like this piece of code, because 
the previous had that fishy "const int&", which raises eyebrows, 
but I think this example could blindsind even the most careful 
programmer.

EDIT: In this specific example, those might be statics, so the 
code would be legal, but what if we replace those ints with 
"foo()"s ?


More information about the Digitalmars-d-learn mailing list