What does ref means
Timon Gehr
timon.gehr at gmx.ch
Wed Sep 7 12:14:22 PDT 2011
On 09/07/2011 08:50 PM, Johannes Totz wrote:
> On 06/09/2011 12:00, bearophile wrote:
>> malio:
>>
>>> Okay, thanks bearophile. But I currently doesn't exactly understand
>>> what's the difference between "ref" and "const ref"/"immutable
>>> ref". If "ref" is syntactic sugar for pointers only (like your
>>> first example), does it also create a copy of the parameters which
>>> are marked as "ref"? I thought that pointers (and in this context
>>> also "ref") avoid the creation of costly copies?!?
>>
>> "ref" just passes a reference to something, so it doesn't perform
>> copies.
>>
>> "const ref" or "immutable ref" just means that you can't change the
>> value (with the usual semantic differences between const and
>> immutable, that are both transitive).
>
> So if a parameter is immutable (without ref) the compiler could infer a
> ref to avoid copy because it can't be modified?
In theory it could. I don't think the current D compilers do that. To
allow it, clear rules would have to be fixed when to apply the
optimization and when not to apply the optimization.
(that is not an issue if the compiler compiles the whole project in one
pass though.)
But probably having to write 'ref' yourself to make calls faster is good
enough, it also does not influence inline assembly in strange and
non-visible ways.
>
>> For the programmer that reads your code, "ref" means the function you
>> have written will usually modify the given argument, while "const
>> ref" means it will not modify it.
More information about the Digitalmars-d-learn
mailing list