Two feature requests
Boris Kolar
boris.kolar at globera.com
Mon Oct 23 07:31:22 PDT 2006
== Quote from Julio César Carrascal Urquijo (jcesar at phreaker.net)'s article
> > auto class Foo { ... }
> > ....
> > Foo x; // equivalent to "auto Foo x;"?
> >
> > .... or perhaps this:
> > Foo x; // equivalent to "auto Foo x = new Foo();"
> I think that this isn't the intended purpose of auto classes in D. They
> are with us to allow deterministic destruction of an instance at the end
> of scope.
> If you are familiar with C# you should think of them as classes that
> implement IDisposable and are created inside a "using" statement. Their
> life-time spans until the closing brace and then the destructor is
> called. This is very useful technique, specially if you are using a
> Database or other resources witch are scarce.
> The C# "using" statement it's more logical to me, but since "auto"
> allows stack allocation (thus improving performance) I'm very happy with
> it and my only complaint it's the type-inference issue.
Deterministic destruction is exactly why I want auto classes. In C++, you also
have deterministic destruction with structs/classes (in case of classes you must
not use the "new" keyword). For example, this C++ code achieves the desired result
of deterministic destruction:
#include "stdio.h"
class Test {
public:
Test() {
printf("ctor\n");
}
~Test() {
printf("dtor\n");
}
};
void test() {
Test t;
}
int main() {
printf("Running test\n");
test();
printf("Done testing\n");
return 0;
}
In C++, however, "auto classes" are much more usefull because they can be passed
as parameters, returned as results, or part of other classes. I'm 100% sure we can
have both: usability without current restrictions and deterministic destruction.
More information about the Digitalmars-d
mailing list