Inferring static array size

Chloé chloekek at use.startmail.com
Sun Apr 28 09:50:05 UTC 2024


On 4/28/24 11:43, NotYouAgain wrote:
> On Sunday, 28 April 2024 at 09:35:22 UTC, Chloé wrote:
>>
>> The Phobos function staticArray can be used to infer the array length 
>> while explicitly specifying (or inferring) the element type:
>>
>>     import std.array : staticArray;
>>     auto a = [1, 2, 3, 4, 5].staticArray;  // inferred int[5]
>>     auto b = [1, 2, 3, 4, 5].staticArray!float;  // inferred float[5]
> 
> Oh. interesting, I didn't know. Thanks.
> 
> but...
> 
> auto a = 20.iota.array.staticArray;
>      //  Error: none of the overloads of template 
> `std.array.staticArray` are callable using argument types `!()(int[])`
> 
> 

To CTFE a range into a static array, try:

     staticArray!(20.iota)

Note the exclamation mark. This uses the following overload:

     auto staticArray(alias a)()
         if (isInputRange!(typeof(a)))



More information about the dip.ideas mailing list