Basically, I am trying to write my own game engine for practice and personal use (I know this is an almost impossible task, but, as I said, it is mainly for learning new things).
I am currently working on my math library (mainly vectors and matrices), and I came across an interesting, but mostly aesthetic problem.
The following pseudo-code is specified:
template <uint8 size>
struct TVector {
float elements[size];
};
Now I want to be able to build a structure with the required number of floats as parameters:
TVector<3> vec0(1.0f, 2.5f, -4.0f);
TVector<2> vec1(3.0f, -2.0f);
TVector<3> vec2(2.0f, 2.2f); // Error: arg missing
TVector<2> vec3(1.0f, 2.0f, 3.0f) // Error: too many args
Since the size of the array is set by the template parameter, I struggled to declare a suitable constructor for the structure. My ultimate goal would be:
TVector(size * (float value));
, , , , 17- ++:
template<typename... Args>
TVector(Args... values) {
static_assert(sizeof...(values) <= size, "Too many args");
uint8 i = 0;
(... , void(elements[i++] = values));
}
( ) , , , , , .
, , , .
, ?
, TVector:
template <const uint8 rows, const uint8 columns>
struct TMatrix {
TVector<rows> elements[columns];
}
, fold ,
.
.
TVector<2> vec(1.0f, 3.0f);
TMatrix<2, 2> mat0(vec, vec); // Works
TMatrix<2, 2> mat1(vec, {0.2f, -4.2f}); // Error
// Does not compile, because the Type is not clear
(, , ).
.
TL; DR: , , :
, 3 , ?
- :
TVector(float... values) {
uint8 i = 0;
(... , void(elements[i++] = values));
}
TMatrix(const TVector<rows>&... values) {
uint8 i = 0;
(..., void(elements[i++] = values));
}
, , , , , .
, :)