ponce
Don
nospam at nospam.com
Wed Nov 10 13:04:46 PST 2010
Steven Schveighoffer wrote:
> On Tue, 09 Nov 2010 17:14:33 -0500, Don <nospam at nospam.com> wrote:
>
>> bearophile wrote:
>>> Jonathan M Davis:
>>>
>>>> it would be possible to make it so that any objects allocated with
>>>> new during CTFE would be in the dynamic heap during runtime.
>>> This is possible, but it doesn't seem what you usually desire when
>>> you allocate an object at compile time.
>>> Bye,
>>> bearophile
>>
>> If it's mutable, it'll go on the heap. If it's immutable, it could
>> optionally go into read-only memory (it will be exactly like the .init
>> of a class instance). Classes which are used only during execution of
>> CTFE functions will not be instantiated at runtime.
>
> Pardon my ignorance, but how can something evaluated at compile time go
> on the heap? The heap doesn't exist yet!
>
> e.g.:
>
> class C
> {
> int[] buffer;
> this() pure { buffer = new int[125];}
> }
>
> C c = new C;
>
> How does c go on the heap at compile time? Won't you have to re-run the
> constructor at runtime to get the right result? Not only that, but even
> if you did run the ctor at compile time, how do you make a copy of c for
> every thread without re-running the ctor?
>
> -Steve
The situation isn't any different to what we already have. You can
already do:
struct F
{
int [] w;
}
F[] foo() {
F[] x = new F[6];
foreach(i; 0..x.length)
x[i].w = new int[20];
return x;
}
and foo() can be run at compile time.
OTOH, I don't know if there is any case where a CTFE value can actually
end up on the heap at runtime. CTFE only kicks in when you need a
manifest constant, and AFAIK there's no way to require a class manifest
constant -- just an element of one.
More information about the Digitalmars-d-learn
mailing list