[英]std::initializer_list as std::array constructor
I need to pass arguments to a wrapper class that looks as minimal example like this: 我需要将参数传递给包装器类,该包装器类看起来像这样的最小示例:
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 init the array with the initializer_list size but nothing works (commented parts of the source) only the constant {5,2,3,4,5,6,7,8,9,10} part does 我试图用initializer_list大小初始化数组,但没有任何效果(源的注释部分),只有常量{5,2,3,4,5,6,7,8,9,10}部分起作用
Someone know the reason and a fix? 有人知道原因和解决方法吗?
Sincerely Matyro 真诚Matyro
Edit 1: The main problem is that TStack does (in most cases) not have a default constructor so i need to initialize the array at construction 编辑1:主要问题是TStack(在大多数情况下)没有默认构造函数,因此我需要在构造时初始化数组
With std::initializer_list
: 使用
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;
};
With std::array
: 使用
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;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.