Why is a static struct's dtor called at the exit of a function?

Jonathan M Davis jmdavisProg at gmx.com
Thu Jul 21 23:38:16 PDT 2011


On Friday 22 July 2011 03:12:00 Diego Canuhé wrote:
> Hi,
> I'm not really an experienced programmer so I might be missing something
> but...
> 
> in
> http://d-programming-language.org/attribute.html
> static is described as follows:
> 
> "The static attribute applies to functions and data. It means that the
> declaration does not apply to a particular instance of an object, but to the
> type of the object. In other words, it means there is no this reference.
> static is ignored when applied to other declarations."
> 
> What I understand from that text is that static only applies to functions
> and data *of a class
> *
> As far as I'm concerned static only makes a method non-virtal and a variable
> a class-variable.
> (static if has nothing to do with this static. I think)
> 
> 
> If I just talked nonsense out of my ignorance, I apologize
> Static data has only one instance for the entire program, not once per objec

static is used for a number of things. In D, it does one of several things:

1. static on a function on a class or struct makes it so that that the 
function has no this pointer/reference, and so you can use it without an 
instance of that object.

2. static on a variable makes it so that the variable exists for the duration 
of the thread that it's on (or for the duration of the program if it's 
shared). So, if you declare a variable static inside of a function, then the 
variable will be unchanged between calls to that function. e.g.

int func()
{
    static int i = 0;
    return i++;
}

func will return a value one greater each time that it's called, since is 
created when the thread is created and it stays around until the thread is 
gone, whereas if it weren't static it would be recreated each time that func 
was called.

3. static on a nested function makes it so that it's a function instead of a 
delegate (that is, it has no access to the function that it's in).

4. A nested struct or class which is _not_ static is tied to the instance of 
an object of its outer class/struct and has access to that object's variables, 
whereas a nested struct or class which _is_ static is not tied to any 
particular instance of its outer class/struct. There can be multiple of them; 
they do not have direct access to their outer class/struct (since they're not 
tied to a particular instance); and they can even be shared between instances 
of their outer class/struct. They're not tied to their outer class/struct at 
all and act more like normal structs or classes.

5. static on a class or struct constructor means that it applies to the class 
as a whole rather than a particular instance of that class or struct, and it 
is run when the thread is initialized. The same goes for static class or 
struct destructors except that they're run when the thread is shut down.

6. static goes on a constructors and destructors at module scope, and they are 
then module constructors. They're run when a thread is created and destroyed 
respectively.

7. static on an if makes it so that the if is run at compile time rather than 
runtime. e.g.

auto func(T)(T val)
{
    static if(Unsigned!T)
        return val * 2;
    else
        return val;
}

If func is used with an unsigned integral value, then it returns the original 
value * 2. Otherwise, it returns the original value.

8. static on an import makes it so that you must explicitly qualify the 
function every time that you use it. e.g.

static import algorithm;
auto found = std.algorithm.find(range, 7);

Just using find doesn't work, because the import was static.


Okay. I _think_ that that's all of them, but I might have missed one. But as 
you can see, static is used for a lot of different things. It all depends on 
the context.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list