<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1894.4">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 16.0px 'Inconsolata-dz for Powerline'}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 16.0px 'Inconsolata-dz for Powerline'; min-height: 21.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px 'Inconsolata-dz for Powerline'; color: #000080}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 16.0px 'Inconsolata-dz for Powerline'; min-height: 21.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 16.0px 'Inconsolata-dz for Powerline'}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 18.0px; font: 16.0px 'Inconsolata-dz for Powerline'; color: #000080; min-height: 21.0px}
p.p7 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px 'Inconsolata-dz for Powerline'; color: #000080; min-height: 21.0px}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Inconsolata-dz for Powerline'; min-height: 21.0px}
p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Inconsolata-dz for Powerline'; color: #808080}
</style>
</head>
<body>
<p class="p1">On 2020-04-30 17:45:24 +0000, Steven Schveighoffer said:</p>
<p class="p2"><br></p>
<p class="p3">No, auto is declaring that there's about to be a variable here. In actuality, auto does nothing in the first case, it just means local variable. But without the type name, the type is inferred (i.e. your second example). This does not do any automatic destruction of your class, it's still left to the GC.</p>
<p class="p4"><br></p>
<p class="p5">Ok, that was my understand too. As said, I found some older posts and was a bit confused...</p>
<p class="p4"><br></p>
<p class="p3">You can use scope instead of auto, and it will then allocate the class on the stack, and destroy it as Ben Jones said. There is danger there, however, as it's very easy to store a class reference elsewhere, and then you have a dangling pointer.</p>
<p class="p4"><br></p>
<p class="p5">Ok. Can't this be combined with some "don't let the refrence escape my function" feature of D?</p>
<p class="p6"><br></p>
<p class="p3">A safer thing to do is:</p>
<p class="p7"><br></p>
<p class="p3">auto X = new MyClass();</p>
<p class="p3">scope(exit) destroy(X);</p>
<p class="p7"><br></p>
<p class="p3">This runs the destructor and makes the class instance unusable, but does not free the memory (so any remaining references, if used, will not corrupt memory).</p>
<p class="p4"><br></p>
<p class="p5">How would that help, because the class instance is now unusable anyway. So I have it around like a zombie and others might think: "Hey you look normal, let's get in contact" and then you are doomed...</p>
<p class="p4"><br></p>
<p class="p3">If your concern is guaranteeing destructors are run, that's what I would pick. If in addition you want guaranteed memory cleanup, then use scope (and be careful).</p>
<p class="p4"><br></p>
<p class="p5">Ok, thanks.</p>
<p class="p8"><br></p>
<p class="p9">--<span class="Apple-converted-space"> </span></p>
<p class="p9">Robert M. Münch</p>
<p class="p9">http://www.saphirion.com</p>
<p class="p9">smarter | better | faster</p>
</body>
</html>