We need a way to make functions pure and/or nothrow based on the purity and/or nothrowability of the functions that they call

Philippe Sigaud philippe.sigaud at gmail.com
Sun Nov 14 07:07:11 PST 2010


On Sun, Nov 14, 2010 at 10:56, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> Does anyone have some good suggestions on how to solve this issue?

IIRC, I posted something related to this 2-3 months ago, though I
don't know if that was a real solution. That was for pure only, but
that could be extended to pure&nothrow. The idea was to try to
instantiate a pure/nothrow test function in the template constraints.
I'm not sure it's much cleaner than your code...

Let see, something like this:

static if (__traits(compiles, {
                                       void test(...) pure { /* fun
body here */}; // can we do a pure fun with this body?
                                   }))
    retType myFunc( whatever ) pure { /* fun body here */ };
else
   retType  myFunc (whatever )         { /* fun body here */ };


That is, you do not need to know the functions called inside myFunc.
You try to instantiate a pure version. If that does not compile, let
myFunc be impure...

Of course, that means repeating the body three times. A partial
solution is to provide myFunc code as a string:

enum funCode = "retType  myFunc (whatever ) %p %n {... };";

then have a template replace %p by "" or "pure" and %n by "" or
"nothrow" and generate four strings.
Then let it try to instantiate the four combinations, beginning with
pure nothrow. When one version compiles, static-if-y it into
existence.
IIRC pure and nothrow can be put at the beginning of the signature? If
that's the case, you do not even need the %n, %p trick. Just prepend
"pure " and such.

the global template would be:

mixin(Instantiate!("@property bool empty() { return innerRange.empty;
}", Pure.yes, Nothrow.yes));

Ok, I typed this rapidly, so feel free to point any mistake in it :-)

Philippe


More information about the Digitalmars-d mailing list