[Challenge] implementing the ambiguous operator in D

Denis Koroskin 2korden at gmail.com
Sun Sep 5 08:59:54 PDT 2010


On Sun, 05 Sep 2010 19:17:55 +0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> 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

Turn main into an "extern(C) int main()" and it works.
Surround the code with try/catch block and it fails again.

Looks like longjmp fails if you run the function inside a try/catch block  
for some reason.


More information about the Digitalmars-d mailing list