On Sunday, 12 October 2014 at 19:46:41 UTC, ketmar via
Digitalmars-d-learn wrote:
Hello.

please, how to call template constructor of a class? it's completely
escaped my mind. i.e. i have this class:

  class A {
    this(alias ent) (string name) {
      ...
    }
  }

and i want to do:

  void foo () { ... }
  auto a = new A!foo("xFn");

yet compiler tells me that

"template instance A!foo A is not a template declaration, it is a class"

I think there's no nice way to explicitly pass template arguments
to constructors. Here's a hack:

auto a = cast(A) (new void[__traits(classInstanceSize, A)]).ptr;
   a.__ctor!foo("xFn");

I didn't think this through or test it thoroughly. So it most
probably has issues. You could try to identify and fix them, and
then hide the ugly away in a function.

But the two leading underscores tell it: "__ctor" is an
implementation detail. It may disappear/change without notice.

Some other solution/workaround would probably be the better
choice. Like a static factory method that's already been
mentioned.

Reply via email to