I am learning OpenGL as an exercise and want to collapse my own math library to get convenient programming using C ++ 11 templates. Solving this problem should not cause runtime - polymorphism.
The main idea is that I want something like this (note that this is clearly invalid C ++ code):
template<class T, int n> //element type is T, size is n
class Vector {
T v1, v2, ... , vn;
public:
Vector(T v1, ... , T vn);
~Vector() noexcept;
...
}
template<T, n>
Vector<T, n> operator +(Vector<T, n> lhs, Vector<T, n> rhs);
...
The problem is that I want to convert these vectors to regular C structures transparently when they are passed as arrays to OpenGL functions. For example, for n == 3I want to convert mine Vector<T, 3>to something like:
template<class T>
struct Vec3 {
T v1, v2, v3;
}
So that I can make the 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);
, Vec3<float>. n == 2, n == 3 n == 4. 3 .
SFINAE : operator T().
public:
operator typename std::enable_if<n == 3, Vec3<T>>::type();
n == 3, :
operator typename std::enable_if<n == 2, Vec2<T>>::type();
operator typename std::enable_if<n == 4, Vec4<T>>::type();
g++ , enable_if ::type typedef 2 4, Vector<float, 3>.
std::array<T, n> , , . , - , std::array<T, n>, Vec3<T>?
std::tuple<class... Types>, . :
- , .
- , .
- -
operator Vec3<T>() . - ,
sizeof(tuple<float, float, float>) == sizeof(Vec3<float>) , () a tuple<float, float, float> Vec3<float>. , g++ stdlib , .