auto arr = [1, 2, 3] should be a static array, not a GC allocated array

Siarhei Siamashka siarhei.siamashka at gmail.com
Wed Jul 20 01:43:17 UTC 2022


On Thursday, 14 July 2022 at 13:14:59 UTC, ryuukk_ wrote:
> It is misleading, nobody expect this to be GC allocated
>
> It should be equal to:
>
> ```D
> int[3] arr = [1, 2, 3]
> ```
>
> Also why it is GC allocated without requiring ``new``?

It is not always GC allocated. LDC optimizer can remove GC 
allocation in the following code (unless such optimization is 
disabled by --disable-gc2stack option):
```D
auto foo()
{
     auto arr = [1, 2, 3];
     return arr[0] + arr[1];
}
```
https://godbolt.org/z/PWTzEbsW8



More information about the Digitalmars-d mailing list