【发布时间】:2019-01-09 11:42:56
【问题描述】:
struct vec2
{
union
{
struct { float x, y; };
struct { float r, g; };
struct { float s, t; };
};
vec2() {}
vec2(float a, float b) : x(a), y(b) {}
};
struct vec3
{
union
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float s, t, p; };
// Here is the problem with g++.
struct { vec2 xy; float z; };
struct { float x; vec2 yz; };
};
vec3() {}
vec3(float a, float b, float c) : x(a), y(b), z(c) {}
};
上面的代码在 Visual Studio 中按预期编译和工作,所以我可以像使用它一样
vec3 v1(1.f, 2.f, 3.f);
vec2 v2 = v1.yz; // (2, 3)
不是在 g++ (MinGW) 中。
src/main.cpp:22:23: error: member 'vec2 vec3::<unnamed union>::<unnamed struct>::xy' with constructor not allowed in anonymous aggregate
src/main.cpp:22:33: error: redeclaration of 'float vec3::<unnamed union>::<unnamed struct>::z'
src/main.cpp:18:30: note: previous declaration 'float vec3::<unnamed union>::<unnamed struct>::z'
src/main.cpp:23:32: error: member 'vec2 vec3::<unnamed union>::<unnamed struct>::yz' with constructor not allowed in anonymous aggregate
src/main.cpp:23:24: error: redeclaration of 'float vec3::<unnamed union>::<unnamed struct>::x'
src/main.cpp:18:24: note: previous declaration 'float vec3::<unnamed union>::<unnamed struct>::x'
我想我一开始就不应该那样做。有什么想法吗?
编辑:在阅读了很多文章并探索了开源项目之后,我开始了解矢量混合应该是什么样的,并在下面发布了解决方案,但仍在等待更好的答案。 p>
编辑 2:所有
vec*成员只能从父级访问,就像 GLM 库一样.
【问题讨论】:
标签: c++ visual-studio g++ glsl mingw