How do I initialize a templated constructor?

Ali Çehreli acehreli at yahoo.com
Mon Aug 8 13:33:23 UTC 2022


On 8/7/22 22:38, rempas wrote:

 > I want to create it and be able to successfully initialize the template
 > parameters
 > of the constructor but until now, I wasn't able to find a way to
 > successfully do
 > that.

The following method uses a convenience function but it's not really needed:

import std.stdio;

struct TestArray(ulong element_n, string type) {
   int[element_n] elements;
   mixin(type) member;
   pragma(msg, "The type is: ", typeof(member));

   this(ulong number) {
     writeln("Constructing with ", number);
   }
}

auto makeTestArray(ulong element_n, string type)(ulong number) {
   return TestArray!(element_n, type)(number);
}

void main() {
   auto ta = makeTestArray!(10, "int")(60);
}

Ali



More information about the Digitalmars-d-learn mailing list