1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
# include <iostream>
# include <array>
# include <iomanip>
static constexpr int n_rows = 3, n_cols = 2;
static constexpr double p_calc(double a, double b) { return a + b; }
static constexpr double t [n_rows] = {0, 10, 20};
static constexpr double r [n_rows][n_cols] = {{0, 1}, {3, 4}, {6, 7}};
namespace detail {
// use std::array or a structure
template <std::size_t Row, std::size_t... Cols>
static constexpr std::array<double, n_cols>
gen_row(std::index_sequence<Cols...>) {
// compute the aggregate object at p[row]
return { p_calc(t[Row], r[Row][Cols])... };
}
template <std::size_t... Rows>
static constexpr std::array<std::array<double, n_cols>, n_rows>
gen_table(std::index_sequence<Rows...>) {
return { gen_row<Rows>(std::make_index_sequence<n_cols>())... };
}
}
static constexpr std::array<std::array<double, n_cols>, n_rows>
gen_table() { return detail::gen_table(std::make_index_sequence<n_rows>()); }
static constexpr auto table = gen_table();
int main() {
for (auto const& row: table) {
for (auto const& col: row)
std::cout << std::setw(2) << col << " ";
std::cout << '\n';
}
}
|