According to what's written in your coliru example, There are a few problems I'd like to point out.
The types of
result
.The following code does not compile.
int main() { int x[] = {1, 2, 3}; int y[3][3] = {x, x, x}; }
whereas the following does.
#include <array> int main() { std::array<int, 3> x = {1, 2, 3}; std::array<std::array<int, 3>, 3> y = {x, x, x}; }
The types of
result
into_array<>
andto_2d_array<>
is equivalent to the first example.The
result
is declared asstatic constexpr
, but the out-of-line definition is missing.
The following is the code with modifications made to fix the issues addressed above.
#include <array>
#include <iostream>
#include <type_traits>
template <typename... Ts>
struct list {};
template <typename List>
struct to_array;
template <typename... Ts>
struct to_array<list<Ts...>> {
using result_type = std::array<unsigned int, sizeof...(Ts)>;
/* Use std::array<> instead of C-array. */
static constexpr result_type result = { Ts::value... };
}; // ToArray<List<Ts...>>
/* Out-of-line definition for static constexpr variable. */
template <typename... Ts>
constexpr typename to_array<list<Ts...>>::result_type
to_array<list<Ts...>>::result;
template <typename List>
struct to_2d_array;
template <typename... Ts>
struct to_2d_array<list<Ts...>> {
using result_type =
std::array<std::array<unsigned int, sizeof...(Ts)>, sizeof...(Ts)>;
/* Use std::array<> instead of C-array. */
static constexpr result_type result = { to_array<Ts>::result... };
};
/* Out-of-line definition for static constexpr variable. */
template <typename... Ts>
constexpr typename to_2d_array<list<Ts...>>::result_type
to_2d_array<list<Ts...>>::result;
int main() {
using my_int_list = list<std::integral_constant<int, 0>,
std::integral_constant<int, 1>,
std::integral_constant<int, 2>>;
for (int i = 0; i < 3; ++i) {
std::cout << to_array<my_int_list>::result[i] << ' ';
} // for
std::cout << std::endl << std::endl;
using my_2d_list = list<my_int_list,my_int_list,my_int_list>;
/* Actually try printing the result. */
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << to_2d_array<my_2d_list>::result[i][j] << ' ';
} // for
std::cout << std::endl;
} // for
}
Prints:
0 1 2
0 1 2
0 1 2
0 1 2
Tested with gcc 4.8.2, clang 3.3 and whichever gcc 4.8 is on Coliru.