Does 'ref' turn into a pointer during compile time?

H. S. Teoh hsteoh at qfbox.info
Thu Dec 22 00:57:04 UTC 2022


On Thu, Dec 22, 2022 at 12:43:28AM +0000, thebluepandabear via Digitalmars-d-learn wrote:
> Say you have the following function that takes in a `ref` parameter:
> 
> ```D
> void modify(ref int num) {
>     num += 5;
> }
> ```
> 
> Does the compiler turn that into the code below?
> 
> ```D
> void modify(int* num) {
>     num += 5;
> }
> ```
> 
> I was just wondering whether or not this is the case because I don't
> think this was touched in the book about D I am reading.

Yes, a ref parameter is implemented as a pointer in the generated
assembly code.  But that's supposed to be an implementation detail that
you generally shouldn't worry about unless you're working with low-level
operations.


T

-- 
Ph.D. = Permanent head Damage


More information about the Digitalmars-d-learn mailing list