我的問題涉及到一個問題我以前問一個一維數組:多維數組的初始化使用索引招
可有人請幫助我延長使用索引竅門多的二維陣列,諸如在本例中:
template<unsigned...> struct indices
{
};
template<unsigned M, unsigned... Is> struct indices_gen
: indices_gen<M - 1, M - 1, Is...>
{
};
template<unsigned... Is> struct indices_gen<0, Is...> : indices<Is...>
{
};
template <typename T>
struct example
{
template<typename ...U, typename
= typename std::enable_if<all_of<std::is_same<U, T>...>::value>::type>
example(U... args)
{
static_assert(3 * 2 == sizeof...(U),
"wrong number of arguments in assignment");
assign(indices_gen<M * N>(), args...);
}
template<size_type... Is, class... U>
void assign(indices<Is...>, U... args)
{
[](...){}(((&array[0][0])[Is] = args)...);
}
T array[3][2];
};
int main()
{
example<int> ex(1, 2, 3, 4, 5, 6);
return 0;
}
目前我取決於要求,即陣列是連續的,但我想分配使用對array
索引,而不只是一個單一的指數(理論值的是我能夠支持數組以外的類型,特別是覆蓋operator[]
)的類型。如果我使用2個參數包進行賦值,我只會在索引(0,0),(1,1),...處分配參數包,如果參數包的長度不相同, array
不同(如在示例中)。
但我認爲,更好的將是有序的笛卡爾發電機對。 – user1095108