Conditionally set nothrow: for a block of code.

Mike Franklin slavo5150 at yahoo.com
Thu May 24 18:51:31 UTC 2018


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

Attempt #1
----------
void test() { }

version(D_BetterC)
{
     nothrow:
}

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

Attempt #2
----------
version(D_BetterC)
{
     enum isNoThrow = true;
}
else
{
     enum isNoThrow = false;
}

void test() { }

static if (isNoThrow)
{
     nothrow:
}

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

Attempt #3
----------
version(D_BetterC)
{
     enum nothrowValue = "nothrow:";
}
else
{
     enum nothrowValue = "";
}

void test() { }

mixin(nothrowValue);

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


Given that the PR above is for object.d, I can't turn the entire 
object.d source file into a string and conditionally mix that in. 
  Does anyone have a solution to this?

Thanks,
Mike


More information about the Digitalmars-d-learn mailing list