No, stack allocation should not destroyed

ryuukk_ ryuukk.dev at gmail.com
Tue Jul 19 11:51:05 UTC 2022


On Tuesday, 19 July 2022 at 11:19:10 UTC, FeepingCreature wrote:
> On Tuesday, 19 July 2022 at 11:00:58 UTC, Hipreme wrote:
>> What happens if I do:
>> ```d
>> void setupRenderer() @nogc
>> {
>>     setUniform("Test", [1, 2]);
>>     setUniform("Test", [1, 2, 3]);
>>     setUniform("Test", [1, 2, 3, 4]);
>> }
>> ```
>>
>> Then what it happens?
>> Error: `@nogc` function `setupRenderer` cannot call non- at nogc 
>> function `renderer.setUniform`
>> Error: `@nogc` function `setupRenderer` cannot call non- at nogc 
>> function `renderer.setUniform`
>> Error: `@nogc` function `setupRenderer` cannot call non- at nogc 
>> function `renderer.setUniform`
>
>>...
>
>> So, for solving that, one must either:
>>
>> `setUniform("Test", [cast(float)1, 2].staticArray);`
>> Or they need to actually have a float literal in the array.
>>
>> Actually, I think that could be one of the the worse usecases 
>> for that
>
> Just to note, even without DIP1000 you can just
>
> void setUniform(string name, float[] vec...) { }
>
> setUniform("Test", 1, 2);
>
> and get a stack allocation for vec.
>
> But what I'd do if I wanted to make fully sure is just `void 
> setUniform(T...)(string name, T args)` and then build a static 
> buffer manually inside `setUniform`.


I don't think that approach is good

I personally use:

```D
void setUniform(string name, scope float[] data);
```

Clearer about what the data is, will always stack alloc and will 
be able to take what ever slice you throw at it

No need vararg or tempaltes that'll end up slowing down your 
compile speed


More information about the Digitalmars-d mailing list