Array initialization with Struct templates

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 31 20:55:26 PDT 2015


Thanks for all the above suggestions, but after many hour of 
re-reading Ali's book on template, structs, and mixins, I still 
in the woods.  I've tried two approaches:

================ Templatetized struct 
================================

struct Chameleon(T, Purpose p)
{
     static if (p == Purpose.POSITIONAL)
     {
         struct Positional { T  x, y, z; }
     }
     else static if (p == Purpose.COLOR)
     {
         struct Color_No_Alpha { T  r, g, b; }
     }
};

struct VertexData
{
     Chameleon!(float, purpose.POSITIONAL)  position;
     Chameleon!(float, purpose.COLOR)  color;
}

alias Vert = VertexData;
alias Pos = VertexData.position;
alias RGB = VertexData.color;

VertexData[] vertices =
[
     VertexData( Pos(1.0, 1.0, 1.0), RGB(0.0, 0.0, 0.0))  // 
Compiler error
];

Error: struct Chameleon does not overload ()

Is this the overloading of the default constructor? Can't I use 
it?  I'm I calling it incorrectly?

	
======================  The Mixin Template approach 
===========================

mixin template Chameleon(T, Purpose p)
{
     static if (p == Purpose.POSITIONAL)
     {
         struct Positional { T  x, y, z; }
     }
     else static if (p == Purpose.COLOR)
     {
         struct Color_No_Alpha { T  r, g, b; }
     }
};

struct VertexData
{
     mixin Chameleon!(float, purpose.POSITIONAL)  position;
     mixin Chameleon!(float, purpose.COLOR)  color;
}

alias Vert = VertexData;
alias Pos = VertexData.position;
alias RGB = VertexData.color;

VertexData[] vertices =
[
     VertexData( Pos(1.0, 1.0, 1.0), RGB(0.0, 0.0, 0.0))
];

returns  Error: function expected before (), not mixin 
Chameleon!(float, cast(Purpose)0) position;

And like above, I'm clueless.




More information about the Digitalmars-d-learn mailing list