i learning opengl exercise, , want roll own math library comfortable programming c++11 templates. solution problem should not invoke runtime-polymorphism.
the basic idea want (note not valid c++ code):
template<class t, int n> //element type t, size n class vector { t v1, v2, ... , vn; public: vector(t v1, ... , t vn); ~vector() noexcept; ... // more constructors , stuff here. } template<t, n> vector<t, n> operator +(vector<t, n> lhs, vector<t, n> rhs); ... // more math functions , operators here... the problem want convert these vectors regular c structs transparently when passed arrays opengl functions. example, n == 3, want convert vector<t, 3> like:
template<class t> struct vec3 { t v1, v2, v3; } so can call:
vector<float, 3> vertices[1]; vertices[0] = vector<float, 3>(1.0f, 1.0f, 1.0f); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw); and have work, if had used array of vec3<float>'s. want behavior n == 2, n == 3, , n == 4. don't want have write 3 classes , implement math operators each one.
my first attempt tried use sfinae casting operator: operator t().
// inside vector's declaration... public: operator typename std::enable_if<n == 3, vec3<t>>::type(); this might have worked fine n == 3, needed:
operator typename std::enable_if<n == 2, vec2<t>>::type(); operator typename std::enable_if<n == 4, vec4<t>>::type(); and g++ complained enable_if didn't have ::type typedef 2 , 4 when instantiated vector<float, 3>.
at point using std::array<t, n> hold values, realized didn't work. not mean values weren't in class, , held somewhere else, passing array of class passting array of std::array<t, n>, not array of vec3<t>?
my current area of interest in using std::tuple<class... types>, since store values directly in class. there few problems idea:
- i want restrict type of tuple 1 type, vector homogenous.
- i want able obtain size of tuple without storing in class.
- i still need implement
operator vec3<t>(), friends somehow. - i don't know if
sizeof(tuple<float, float, float>) == sizeof(vec3<float>), or guarentees memory layout allows me safely (say) casttuple<float, float, float>vec3<float>. i've heard g++'s stdlib stores tuple values in reverse order inside class, example.
std::array<t, n> guaranteed zero-overhead wrapper on t[n], can store in class , sure there's no padding mess opengl calls.
Comments
Post a Comment