| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
January 21, 2015 Some array casts | ||||
|---|---|---|---|---|
| ||||
Currently this is accepted: int[2] m = cast(int[2])[1, 2]; But Kenji suggests that the cast from int[] to int[2][1] should not be accepted. Do you know why? Reference: https://issues.dlang.org/show_bug.cgi?id=7514 Bye and thank you, bearophile | ||||
January 21, 2015 Re: Some array casts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 21 January 2015 at 14:31:15 UTC, bearophile wrote:
> Currently this is accepted:
>
> int[2] m = cast(int[2])[1, 2];
>
> But Kenji suggests that the cast from int[] to int[2][1] should not be accepted. Do you know why?
>
> Reference:
> https://issues.dlang.org/show_bug.cgi?id=7514
>
> Bye and thank you,
> bearophile
This is because of the .sizeof property, for example:
void main(string[] args)
{
int[3] m = cast(int[3])[1, 2, 3];
writeln(m.sizeof);
writeln([1, 2, 3].sizeof);
}
outputs 12 / 8
Your previous example is corner case, it's 8/8 in both.
| |||
January 21, 2015 Re: Some array casts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Baz | On Wednesday, 21 January 2015 at 14:41:48 UTC, Baz wrote:
> On Wednesday, 21 January 2015 at 14:31:15 UTC, bearophile wrote:
>> Currently this is accepted:
>>
>> int[2] m = cast(int[2])[1, 2];
>>
>> But Kenji suggests that the cast from int[] to int[2][1] should not be accepted. Do you know why?
>>
>> Reference:
>> https://issues.dlang.org/show_bug.cgi?id=7514
>>
>> Bye and thank you,
>> bearophile
>
> This is because of the .sizeof property, for example:
>
> void main(string[] args)
> {
> int[3] m = cast(int[3])[1, 2, 3];
> writeln(m.sizeof);
> writeln([1, 2, 3].sizeof);
> }
>
> outputs 12 / 8
>
> Your previous example is corner case, it's 8/8 in both.
with -m32 of course.
| |||
January 21, 2015 Re: Some array casts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Baz | Baz:
> int[3] m = cast(int[3])[1, 2, 3];
> writeln(m.sizeof);
> writeln([1, 2, 3].sizeof);
The sizeof values aren't relevant for D array casts. What matters are the array "contents".
Bye,
bearophile
| |||
January 21, 2015 Re: Some array casts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | > The sizeof values aren't relevant for D array casts. What matters are the array "contents".
See:
void main() {
int[5] m = cast(int[5])[1, 2, 3, 4, 5];
assert(m == [1, 2, 3, 4, 5]);
pragma(msg, m.sizeof); // 20u
pragma(msg, [1, 2, 3, 4, 5].sizeof); // 8u
}
Bye,
bearophile
| |||
January 21, 2015 Re: Some array casts | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 21 January 2015 at 18:08:44 UTC, bearophile wrote:
>> The sizeof values aren't relevant for D array casts. What matters are the array "contents".
>
> See:
>
> void main() {
> int[5] m = cast(int[5])[1, 2, 3, 4, 5];
> assert(m == [1, 2, 3, 4, 5]);
> pragma(msg, m.sizeof); // 20u
> pragma(msg, [1, 2, 3, 4, 5].sizeof); // 8u
> }
>
>
> Bye,
> bearophile
i'm sorry if I've lead you on a wrong way.
My bad.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply