被呛我难过,回家反省,确定这个直接 template-meta 的版本没人给过
#include <cstdio>
template <int N, int M> struct p {
static void foo() {
printf("%2d x %2d = %2d\n", N, M, N * M); p<N, M + 1>::foo();
}
};
template <int N> struct p<N, 9> {
static void foo() {
printf("%2d x %2d = %2d\n", N, 9, N * 9); p<N + 1, 1>::foo();
}
};
template <> struct p<9, 9> {
static void foo() {
printf("%2d x %2d = %2d\n", 9, 9, 9 * 9);
}
};
int main() {
p<1, 1>::foo();
}
等等来试试看 main recursive