ChainImpl save method

Steven Schveighoffer schveiguy at yahoo.com
Thu Dec 30 07:23:34 PST 2010


On Wed, 29 Dec 2010 17:35:05 -0500, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> One more question. What does this do?
>
> static RvalueElementType fixRef(RvalueElementType val)
> {
>     return val;
> }
>
> This looks like a workaround for something but I can't figure out why
> the function is static and why does it just return the value passed
> in?

A static function inside a struct or class does not have a 'this'  
reference.  It's like a free function (one outside a class or struct  
definition) but is within the namespace of the struct or class.

Why it is there?  I think because a chain range's components may all  
return by ref, or at least one may return by value.  If at least one  
returns by value, it logically follows that the chain range itself must  
return by value.  If all return by ref, it's ok for the chain range to  
return by ref.

So why does the fixRef exist?  Probably because the compile-time checks  
used to determine whether you can return by ref or return by value are  
verbose, and fixRef is used in many places.  I'm only speculating, I  
didn't write it.  With proper compiler inlining, the fixRef function gets  
optimized out, but I think ref functions do not get inlined at the moment.

-Steve


More information about the Digitalmars-d-learn mailing list