Reputation: 5805
https://dlang.org/spec/expression.html says "If a NewExpression is used as an initializer for a function local variable with scope storage class, and the ArgumentList to new is empty, then the instance is allocated on the stack rather than the heap or using the class specific allocator."
Does it mean that in the following program an object of C
is allocated entirely on the stack, without any heap allocation?
class C {
int x;
}
void main() {
scope c = new C();
}
Also: Why does it work only for empty argument list?
I doubt that I understand correctly, because all the rest D materials I read say that classes are allocated on the heap. I want to make sure.
Upvotes: 1
Views: 155
Reputation: 1228
In D, generally, classes are allocated on the heap. In this one specific case, classes can be allocated on the stack.
The specific case is:
new SomeClass
scope
)new(args) SomeClass
.Upvotes: 4