I tried to implement an std::initializer_list
constructor for my lightweight array class, but then I immediately found a problem that std::initializer_list
is a constexpr
. I did solve it somehow by using variadic template constructors and helper functions, but it feels like an overkill (IMO) for this single purpose. My experience with C++ is short so I believe there is a better and simpler way.
I also thought about using a move constructor, from an std::array
? but the concept of move expressions is still unclear to me. Anyway, is this also worth considering?
Any good solution or suggestion would help me.
template<typename T>
class Array:private boost::scoped_array<T>{
private:
int m_length;
int CountArguments(){
return 0;
}
template<typename... Ts>
int CountArguments(T const& t,Ts const&... ts){
return 1+CountArguments(ts...);
}
void FillFromArguments(T* p){}
template<typename... Ts>
void FillFromArguments(T* p,T const& t,Ts const&... ts){
*p++=t;
FillFromArguments(p,ts...);
}
public:
Array(){}
Array(int length):
boost::scoped_array<T>(new T[length]),
m_length(length){}
template<typename... Ts>
Array(T const& t,Ts const&... ts){
m_length=CountArguments(t,ts...);
boost::scoped_array<T>::reset(new T[m_length]);
FillFromArguments(boost::scoped_array<T>::get(),t,ts...);
}
/* ... */
};