Thread overview
std.c.stdlib (atexit)
April 07, 2004
hi, again! i need some help... i am doing a singleton class (templated with Destructors).
the problem is that i don't know how to pass a D pointer to function to the c function atexit (std.c.stdlib)!!!

[compiler says: function atexit (void(C*)()) does not match argument types (void(*)()) ]

    private import std.c.stdlib;
    extern (C) int atexit(void (*)());

    interface IDestructor {
    public:
        void create(void (*dtor)());
    }

    class DestructorAtExit : IDestructor {
    public:
        void create(void (*dtor)()) { atexit(dtor); }
    }

thanks,
Miguel Ferreira Simões


April 07, 2004
In article <c51gsn$omq$1@digitaldaemon.com>, =?iso-8859-1?Q?Miguel_Ferreira_Sim=F5es?= says...
>
>hi, again! i need some help... i am doing a singleton class (templated =
>with Destructors).
>the problem is that i don't know how to pass a D pointer to function to =
>the c function atexit (std.c.stdlib)!!!
>
>[compiler says: function atexit (void(C*)()) does not match argument =
>types (void(*)()) ]
>
>    private import std.c.stdlib;
>    extern (C) int atexit(void (*)());
>
>    interface IDestructor {
>    public:
>        void create(void (*dtor)());
>    }
>
>    class DestructorAtExit : IDestructor {
>    public:
>        void create(void (*dtor)()) { atexit(dtor); }
>    }
>
>thanks,
>Miguel Ferreira Sim=F5es
>

This works for me:

private import std.c.stdlib;
alias void function () c_func;

extern (C) int atexit(c_func);

interface IDestructor {
public:
void create(c_func);
}

class DestructorAtExit : IDestructor {
public:
void create(c_func dtor) { atexit(dtor); }
}

import std.c.stdio;
void foo() { printf("bye"); }

void main() {
(new DestructorAtExit).create( &foo );
}

-------------------
Carlos Santander B.
April 07, 2004
thanks, it works!!!