Array initialization with Struct templates
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 30 22:38:42 PDT 2015
On Monday, August 31, 2015 04:57:05 WhatMeWorry via Digitalmars-d-learn wrote:
>
> 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.
VertexData doesn't have a constructor that takes 6 doubles or 6 floats. It
has a compiler-generated constructor that's equivalent to
this(Chameleon!(float, purpose.POSITIONAL) position,
Chameleon!(float, purpose.COLOR_ONLY) color)
{
this.position = position;
this.color = color;
}
So, you're going to need to pass it a Chameleon!(float, purpose.POSITIONAL)
and a Chameleon!(float, purpose.COLOR_ONLY color), not 6 doubles - either
that, or you're going to need to declare a constructor for VertexData which
takes 6 doubles or floats and converts them to what's require to assign to
its member variables.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list