How do I initialize a templated constructor?

Dom Disc dominikus at scherkl.de
Mon Aug 8 11:03:21 UTC 2022


On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote:
> On Monday, 8 August 2022 at 05:38:31 UTC, rempas wrote:
>> In the following struct (as an example, not real code):
>>
>> ```
>> struct TestArray(ulong element_n) {
>>   int[element_n] elements;
>>
>>   this(string type)(ulong number) {
>>     pragma(msg, "The type is: " ~ typeof(type).stringof);
>>   }
>> }
>> ```
>
> You cannot do this.

But if you only want to know the type of the parameter, you can 
do this:

```D
struct TestArray(ulong element_n) {
   int[element_n] elements;

   this(type)(type number)
   {
     pragma(msg, "The type is: " ~ type.stringof);
   }
}
```



More information about the Digitalmars-d-learn mailing list