Hi everybody,
I'm working on a vector class - the mathematical kind - for C++11. I'm trying to find a decent way for initialization, such as:
My first idea was to use an initializer list. However, as size() is not a constexpr (Why? I've looked around and it seems possible to implement, does it cause any additional issues?), the number of arguments isn't verified at compile time.Code:Vector<3> v{ 0, 1, 2 };
So I looked into the std::array class. It seems to simply make the array a public member of the class (actually, struct), such that it can be initialized similarly to the above code, except that it changes the assignment syntax to:
Except for assignment, that is. This works fine, but it becomes messy for multi-dimensional arrays (or, matrices), and I don't like the syntax.Code:std::array<int, 3> v{{ 0, 1, 2 }};
So I wrote up a quick test, and it looks like this:
Then, the vector class has a constructor:Code:template<typename ArrayType, size_t ELEMENTS> class ArrayAssign { public: template<typename... Args> static inline void assign(ArrayType array, Args... args) { static_assert(sizeof...(args) == ELEMENTS, "Invalid number of parameters."); _assign(array, 0, args...); } private: template<typename... Args> static inline void _assign(ArrayType array, size_t i, const decltype(ArrayType()[0])& v, Args... args) { array[i] = v; _assign(array, i+1, args...); } static inline void _assign(ArrayType array, size_t i, const decltype(ArrayType()[0])& v) { array[i] = v; } };
This actually works as I expected. The first code snippet works, and the initialization syntax looks a lot clearer to me.Code:template<size_t N, typename Type = double> class Vector { public: template<typename... Args> Vector(Args... args) { ArrayAssign<Type*, N>::assign(v, args...); } ...
I haven't checked if I can get it to work for matrices as well though, there are some issues as it is now (I'll look into it).
My questions:
1. Are there any side effects that I'm missing, causing this method to be inferior to the method std::array uses?
2. What *are* the issues with std::initializer_list::size() being constexpr?
Thanks in advance



LinkBack URL
About LinkBacks


