Dynamic array creation with default

Timon Gehr timon.gehr at gmx.ch
Wed Aug 24 14:53:35 PDT 2011


On 08/24/2011 11:19 PM, Steven Schveighoffer wrote:
> On Wed, 24 Aug 2011 15:15:54 -0400, Michel Fortin
> <michel.fortin at michelf.com> wrote:
>
>> On 2011-08-24 11:17:08 +0000, "Steven Schveighoffer"
>> <schveiguy at yahoo.com> said:
>>
>>> It's actually possible, but ugly:
>>> auto ptr = (new int[10][](1)).ptr;
>>> One really interesting thing to note -- the compiler actually turns
>>> struct allocations into array-of-one allocations in the runtime. So
>>> this is likely what the compiler would do if it supported direct heap
>>> allocation of fixed-sized arrays.
>>
>> A problem with this approach is that it won't work in safe mode
>> because accessing the .ptr property of an array is unsafe in general.
>> But it is perfectly safe to allocate a static array on the GC heap, so
>> it should be allowed…
>
> And what would be the type returned by such a function? ;)
>
> -Steve

int[N]*


More information about the Digitalmars-d mailing list