scope exit in mixin template

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 8 12:42:13 PDT 2014


On Sunday, 8 June 2014 at 18:48:03 UTC, monarch_dodra wrote:
> Mixin templates can only insert declarations, not arbitrary 
> code. When it sees "scope", it's expecting it to be the 
> attribute, not the declaration.

To add to that, if you want to mixin arbitrary code, then you can 
use a string mixin:

template declare_bar(string var_name) {
   enum declare_bar =
     "auto " ~ var_name ~ " = c_make();" ~
     "scope(exit) if(" ~ var_name ~ ") c_free(" ~ var_name ~ ");"
}

void main() {
     mixin(declar_var!"b")
}

For example.

That said, given your example, simply using an RAII wrapper 
*could* be superior (depends on your actual usecase).


More information about the Digitalmars-d-learn mailing list