I have a small Slice class that I use for functions that want to take a 'list' of items. It doesn't store any items, but is just a view into some existing list.
#include <initializer_list>
template<typename T>
struct Slice
{
u32 length;
T* data;
Slice(T& element) { length = 1; data = &element; }
template<typename T, u32 Length>
Slice(T(&arr)[Length]) { length = Length; data = arr; }
inline T& operator [](u32 i) { return data[i]; }
};
I'm trying to make it possible to construct from an initializer list. Like so
Slice<i32> indices = { 0, 1, 2, 2, 3, 0 };
So I tried adding a constructor that takes an initializer_list
template<typename T>
Slice(std::initializer_list<T> initList) { length = initList.size(); data = (T*) initList.begin(); }
But, I think this is ill-formed because the initializer_list
is a temporary value that will go out of scope after the assignment. Further, the Slice data is not strictly const and it appears that initializer_lists are.
Is there a proper way to implement this behavior?