Std :: initializer_list as std :: array constructor

I need to pass arguments to a wrapper class that looks like a minimal example:

template<class TStack, unsigned int TBins> class Wrapper : Stack<......> { std::array<TStack, TBins> m_oStacks; template<typename ... Args> Wrapper(std::initializer_list<const unsigned int> const& size, Args&&... args) : Stack<.......>(args), m_oStacks{5,2,3,4,5,6,7,8,9,10} //, m_oStacks(size) //,m_oStacks{size} //,m_oStacks{{size}} { //m_oStacks = { size }; } }; 

I tried to initialize an array with the size of initializer_list, but nothing works (the commented parts of the source) only part of the constant {5,2,3,4,5,6,7,8,9,10} does

Does anyone know the reason and fix?

Regards Matyro

Edit 1: The main problem is that TStack (in most cases) does not have a default constructor, so I need to initialize the array when building

+1
source share
1 answer

With std::initializer_list :

 template <typename TStack, std::size_t TBins> class Wrapper { public: Wrapper(std::initializer_list<unsigned int> il) : Wrapper(il, std::make_index_sequence<TBins>{}) { } private: template <std::size_t... Is> Wrapper(std::initializer_list<unsigned int> il, std::index_sequence<Is...>) : m_oStacks{{ *(il.begin() + Is)... }} { } std::array<TStack, TBins> m_oStacks; }; 

Demo

With std::array :

 template <typename TStack, std::size_t TBins> class Wrapper { public: Wrapper(const std::array<unsigned int, TBins>& a) : Wrapper(a, std::make_index_sequence<TBins>{}) { } private: template <std::size_t... Is> Wrapper(const std::array<unsigned int, TBins>& a, std::index_sequence<Is...>) : m_oStacks{{ a[Is]... }} { } std::array<TStack, TBins> m_oStacks; }; 

Demo 2

+2
source

Source: https://habr.com/ru/post/1243513/


All Articles