[Challenge] implementing the ambiguous operator in D

bearophile bearophileHUGS at lycos.com
Sun Sep 5 08:17:55 PDT 2010


Denis Koroskin:
> The code above should print:
> state saved
> state restored
> 
> (not tested)

On Windows, dmd 2.048, this code:


import std.c.stdio: puts;

version (Windows) {
    alias int[16] jmp_buf;
    extern (C) extern {
        int setjmp(ref jmp_buf env);
        void longjmp(ref jmp_buf env, int value);
    }
}

struct State {
     int save() {
         return setjmp(buf);
     }

     void restore(int status) {
         assert(status != 0);
         longjmp(buf, status);
     }

     private jmp_buf buf;
}

void main() {
     State state;
     int result = state.save();
     if (result == 0) {
         // first time here
         puts("state saved");
         state.restore(1);
     } else {
         assert(result == 1);
         puts("state restored");
     }
}


Gives an Access Violation after printing state saved.

Bye,
bearophile


More information about the Digitalmars-d mailing list