7

I'm in the process of porting, enhancing, and D-atizing our reign SDK from C# to D. Currently working on the Vector2 math module.

Will there be any performance difference between the two structs below? My benchmarks show identical performance, but I'd like to gain a bit of expert insight :)

struct Vector2(T)
{
    T x, y;
    @property T u() { return x; }
    @property T v() { return y; }
    @property void u(T value) { x = value; }
    @property void v(T value) { y = value; }
}

struct Vector2(T)
{
    union { T x, u; }
    union { T y, v; }
}

Obviously I'd like to use the unions for syntactical simplicity. But is there any unforeseen pitfalls with using them? I'm unfamiliar with their low-level details.

On a side note, I'm adding in vector property syntax similar to HLSL/GLSL, e.g., vec1.yxz += vec2.xyz; There's... no.. possible way to do that with unions instead of @property is there?

4

1 回答 1

13

使用alias

struct Vector2(T)
{
    T x, y;
    alias x u;
    alias y v;
}
于 2011-12-31T09:14:20.640 回答