Conditionally set nothrow: for a block of code.

Mike Franklin slavo5150 at yahoo.com
Tue May 29 00:17:48 UTC 2018


On Thursday, 24 May 2018 at 18:51:31 UTC, Mike Franklin wrote:
> I'm trying to find a way to declare a block of code `nothrow:` 
> when compiling with -betterC, but not `nothrow` when not 
> compiling with -betterC.
>
> The solution is needed for this PR:  
> https://github.com/dlang/druntime/pull/2184/files#r188627707

Walter came up with a solution to this; put the code that is 
conditionally `nothrow` in a template and mix it in in the 
appropriate branch.

void test() { }

mixin template code()
{
     extern(C) void main()
     {
         test();   // This should throw an error in -betterC 
because `main` is
                   // `nothrow` but `test` isn't. It doesn't work
     }
}

version(D_BetterC)
{
     nothrow:
     mixin code;
}
else
{
     mixin code;
}

Thanks, Walter.

Mike




More information about the Digitalmars-d-learn mailing list