3

現在、initializer_list の初期化を受け入れない多次元配列がありますが、それを許可したいと考えています。ただし、テンプレート引数に基づいて std::initializer_list に任意のネスト数を指定することはできないようです。

// e.g. 5D array:
array<int, 5> arr(10, 5, 20, 34, 10); // the integers are the lengths of each dimension.

// This is what I'm trying to achieve on top of the above:
// creates the array and initializes it
array<int, 5> arr{ {{{{0, 1}}}}, {{{{1, 1}}}} };

// class signature:
// template <template T, unsigned dimensions> array { ... }
// --> where T is the array element type

答えは必ずしも を使用する必要はありませんstd::initializer_list

4

1 に答える 1

0

私はこれがうまくいくと思います:

template<typename BASE, int N>
struct nested {
  typedef std::initializer_list<typename nested<BASE,N-1>::initializer_list> initializer_list;
};

template<typename BASE>
struct nested<BASE,0> {
  typedef BASE initializer_list;
};


template<typename BASE, int N>
struct multi {
  multi(typename nested<BASE,N>::initializer_list& init) {
  };
};

残念ながら、私のバージョンの gcc はどちらも initializer_list をサポートしていないため、これを適切にテストすることはできません。

于 2013-01-14T01:22:13.480 に答える