This aspect makes it easy to write generalized tools for any vector size, again without specializations
For example, this function, which serializes a vector into a json object and returns it.
This is different from other math libraries, like
glm, where although code like this is possible to write, because many vector types are manually specialized, it makes development of generic code like this much harder.
template<size_t S, typename T>
SerializedData<vec<S, T>> serialize(const vec<S, T>& v) {
SerializedData<vec<S, T>> res{};
if constexpr (S > 4) {
// If vectors are too big to have xyzw components, save it as an array
for (int i = 0; i < S; ++i)
res.as_array().push_back(v[i]);
}
else {
// For sizes of 4 or less, save vector data in named keys
res["x"] = v.x;
if constexpr (S >= 2)
res["y"] = v.y;
if constexpr (S >= 3)
res["z"] = v.z;
if constexpr (S >= 4)
res["w"] = v.w;
}
return res;
}