Array initialization with Struct templates

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 30 21:57:05 PDT 2015


This seemingly trivial array initialization has caused me hours 
of grief.

enum Purpose { POSITIONAL, COLOR_ONLY, COLOR_AND_ALPHA, 
GENERIC_TRIPLE, GENERIC_QUAD }
Purpose purpose;

struct Chameleon(T, Purpose p)  // template
{
     static if (is (p == POSITIONAL)) {
         T  x, y, z;
     } else static if (is (p == COLOR_ONLY)) {
         T  r, g, b;
     } else static if (is (p == COLOR_AND_ALPHA)) {
         T  r, g, b, a;
     }  else static if (is (p == GENERIC_TRIPLE)) {
         T  a, b, c;
     } else static if (is (p == GENERIC_QUAD)) {
         T  a, b, c, d;
     }
};

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

alias Vert = VertexData;

VertexData[] vertices =
[
     Vert(1.0, 1.0, 1.0, 0.0, 0.0, 0.0)  // compiler error here
];

I keep getting:

Error: cannot implicitly convert expression (1.00000) of type 
double to Chameleon!(double, cast(Purpose)0)	

I even tried  Vert(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f)

but it has the exact same error.  Any ideas?  Thanks in advance.




More information about the Digitalmars-d-learn mailing list