Array of templated classes or structs

qsdfghjk via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 24 10:20:07 PDT 2015


On Saturday, 24 October 2015 at 17:06:13 UTC, Dandyvica wrote:
> On Saturday, 24 October 2015 at 16:58:58 UTC, qsdfghjk wrote:
>> On Saturday, 24 October 2015 at 15:57:09 UTC, Dandyvica wrote:
>>> Hi guys,
>>>
>>> Apart from deriving from the same class and declaring an 
>>> array of that
>>> root class, is there a way to create an array of templates?
>>>
>>> This seems not possible since template are compile-time 
>>> generated, but just to be sure. For example, it seems logical 
>>> to get an array of complex numbers but Complex needs to be 
>>> declared with a type.
>>>
>>> Thanks for any hint.
>>
>> You can declare an array whose the element type matches to one 
>> of the template parameter:
>>
>> ---
>> struct Foo(T)
>> {
>>     Foo!T[] foos;
>>     // typeof(this)[] foos // equivalent
>> }
>> ---
>>
>> since it's an array (fixed size whatever is the element type: 
>> size_t len + size_t pointer) it doesn't matter if the template 
>> declaration is partial.
>
> In that case, all elements have the same type right? I'd like 
> different types,
> but with the same template.

Then no, it's not possible, although some ugly workaround may 
allow the thing (array of pointer and something used to cast the 
pointer at runtime, like an AliasSeq). But by definition if you 
have serveral type in an aray it's not array...it's an agregate.


More information about the Digitalmars-d-learn mailing list