When is a dynamic array really a static array?
Steven Schveighoffer
schveiguy at gmail.com
Tue Dec 31 18:28:36 UTC 2019
On 12/31/19 12:24 PM, Temtaime wrote:
> On Tuesday, 31 December 2019 at 13:35:42 UTC, Paul Backus wrote:
>> On Tuesday, 31 December 2019 at 11:20:31 UTC, Patrick Schluter wrote:
>>> Is it really a bug?
>>
>> Yes. Perhaps this example will convince you:
>>
>> void foo(size_t N)(int[N] arr)
>> {
>> arr[0] = 42;
>> }
>>
>> void foo()(int[] arr)
>> {
>> arr[0] = 42;
>> }
>>
>> void main()
>> {
>> int[16] x;
>> foo(x[]);
>> assert(x[0] == 42); // fails
>> }
>
> void foo(size_t N)(int[N] arr) does nothing. I doubt this is a bug too.
int[N] foo(size_t N)(int[N] arr)
{
arr[0] = 42;
return arr;
}
Better?
The use case that spurred this discovery is this:
ubyte[16] hash;
string hexStr = toHexString(hash); // slices a temporary that is
immediately removed from scope
The fix should be:
string hexStr = toHexString(hash[]);
But it doesn't do anything different. In order to do it correctly, you
have to literally declare another variable, or cast. Hell, I don't even
know if the cast will work!
-Steve
More information about the Digitalmars-d
mailing list