On 5/15/18 7:53 AM, Yuxuan Shui wrote:
On Friday, 11 May 2018 at 18:55:03 UTC, Meta wrote:
On Friday, 11 May 2018 at 15:03:41 UTC, Uknown wrote:
[...]

It's not as pretty, and I don't know if it works outside this toy example yet, but you can do:

import std.stdio;

struct Allocator
{
    auto call(alias F, Args...)(Args args)
    {
        return F(this, args);
    }

    void deallocateAll()
    {
        writeln("deallocateAll");
    }
}

void f1(Allocator a, int n) { writeln("f1"); }
void f2(Allocator, string s, double d) { writeln("f2"); }

void main()
{
    with (Allocator())
    {
        scope(exit) deallocateAll;
        call!f1(2);
        call!f2("asdf", 1.0);
    }
}

I found another alternative to this:

https://godbolt.org/g/3Etims

Hm... neat idea. Somehow, opDispatch can probably be used to make this work even more generically (untested):

struct WithAlloc(alias alloc)
{
auto opDispatch(string s, Args...)(auto ref Args args) if (__traits(compiles, mixin(s ~ "(args, alloc)")))
   {
      mixin("return " ~ s ~ "(args, alloc);");
   }
}

-Steve

Reply via email to