D newb

Steven Schveighoffer schveiguy at yahoo.com
Sun Aug 17 19:26:41 PDT 2008


"JAnderson" wrote
> Steven Schveighoffer wrote:
>> "bearophile" wrote
>>> Robert Fraser:
>>>> scope Foo f = new Foo();
>>>>
>>>> The "scope" keyword in the declaration says to the compiler "this will
>>>> not escape the function" thus creating the data on the stack. "f" is
>>>> still a reference type in this case, however (although it may not be
>>>> implemented as a reference -- that's up o the compiler).
>>> But the 'scope' works only in some situations, the original poster can 
>>> take a look at the docs about 'scope'.
>>>
>>> My question: does the compiler raise a compilation error if the 'scope' 
>>> keyword is used where the compiler can't actually allocate the class on 
>>> the stack? (if not, then I think it may be better for the compiler to 
>>> raise such error, avoiding the illusion of efficiency).
>>
>> In what situations does it not work?
>>
>> As long as the class is not too large for the stack (which is very 
>> unlikely), it should be able to allocate.
>>
>> -Steve
>
> If the class has a component sub-class the component may be heap 
> allocated.  I don't agree with making that an error though.

If the class allocates anything dynamically, it would be heap allocated.  I 
think there's no way around that.

for instance, C++'s vector class can be stack allocated, but the elements 
will be heap allocated.

However, a frequently asked-for enhancement is to allow scope members which 
could be allocated as part of the class' space (and therefore would be 
allocated on the stack in the case of a scope allocated class).

-Steve 





More information about the Digitalmars-d mailing list