我正在尝试解压缩多个参数包,以便arr[i][j] = X(i,j)
. 但是,使用下面的当前方法出现以下错误。有没有使用 C++14/17 实现上述目标的方法?
// Non copyable and default constructable
struct X
{
X(const X&) = delete;
X(int x, int y) : _x(x), _y(y)
{}
int _x;
int _y;
};
X getX(int i, int j)
{
return X(i,j);
}
template <std::size_t ...Xidx, std::size_t ... Yidx>
auto make_2d_array(std::index_sequence<Xidx...>, std::index_sequence<Yidx...>)
{
std::array<std::array<X,sizeof...(Xidx)>, sizeof...(Yidx)> arr = {getX(Xidx, Yidx)...};
return arr;
}
static constexpr std::size_t M = 5;
static constexpr std::size_t N = 6;
int main()
{
std::array<std::array<X,M>, N> arr = make_2d_array(std::make_index_sequence<M>{}, std::make_index_sequence<N>{});
}
错误
<source>:26:87: error: pack expansion contains parameter packs 'Xidx' and 'Yidx' that have different lengths (5 vs. 6)
std::array<std::array<X,sizeof...(Xidx)>, sizeof...(Yidx)> arr = {getX(Xidx, Yidx)...};
~~~~ ~~~~ ^
<source>:35:42: note: in instantiation of function template specialization 'make_2d_array<0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5>' requested here
std::array<std::array<X,M>, N> arr = make_2d_array(std::make_index_sequence<M>{}, std::make_index_sequence<N>{});
^
1 error generated.
Compiler returned: 1