Template delegate/function ptr struct member

ed via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 13 00:28:58 PDT 2014


I'm porting some C++ code to D and a struct has the following 
member:

struct S
{
// ...
  //void* (*createMethod)();

void* function() createMethod;

}

I'd like to extend this as little to accept delegates for future 
use without breakage to existing code...

Is it possible to template this so "createMethod" can be a 
delegate() or function() of any type *and* have the compiler 
infer the type from ctor parameters?

For example:
---
struct S(F)
{
     // ...
     F createMethod;
     this(alias F)() {createMethod = &F;}
}
static void* func() {return null;}

void main() {
     auto s1 = S(&func);
     auto s2 = S(&Object.classinfo.create);

     // s1 is S!(void* function())
     // s2 is S!(Object delegate() const);
}
---

Is this a good idea or am I way off?

Thanks,
ed






More information about the Digitalmars-d-learn mailing list