C++ Meta Programming
网上看到一个99乘法表,代码不全不知道这个代码怎么写的
#include
using namespace std;
inline void printLine(int i, int j) {
cout << i << " * " << j << " = " << i * j << endl;
}
template
struct Line {
inline static void print() {
Line::print();
printLine(I, J);
}
};
template
struct Line {
inline static void print() {
printLine(I, 1);
}
};
template
struct Group {
inline static void print() {
Group::print();
cout << endl;
Line::print();
}
};
template
struct Group {
inline static void print() {
Line<1, NUM>::print();
}
};
template
struct Meta99 {
inline static void print() {
Group::print();
}
};
int main() {
Meta99<20>::print();
return 0;
}
#include <iostream>
inline void print_item (size_t const i, size_t const j)
{
std::cout << i << "x" << j << "=" << i * j << " ";
}
template <size_t I, size_t J>
struct line_t
{
inline static void print ()
{
print_item(I,J);
line_t<I+1,J>::print();
}
};
template <size_t I>
struct line_t<I,I>
{
inline static void print ()
{
print_item(I,I);
}
};
template <size_t N>
struct table_t
{
inline static void print ()
{
table_t<N-1>::print();
std::cout << std::endl;
line_t<1,N>::print();
}
};
template <>
struct table_t<1>
{
inline static void print ()
{
line_t<1,1>::print();
}
};
template <size_t N>
struct meta99
{
inline static void print ()
{
table_t<N>::print();
std::cout << std::endl;
}
};
int main ()
{
meta99<9>::print ();
return 0;
}