moorec's asdf dir #1

Oct 14, 2011 at 5:59pm
I was looking through some old directories this afternoon and stumbled upon a few programs that I had written and saved for reference. I figured it might be fun to hear your critique.

First, as a response to the common homework assignment to make a triangle of asterisks is this template metaprogram:
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
#include <iostream>

template< int N >
struct Row {
    std::string value;
    Row() { value += '*' + Row< N - 1 >().value; }
};
template<>
struct Row<0> {
    std::string value;
    Row() { value += '\n'; }
};

template< int N >
struct Pyramid {
    std::string value;
    Pyramid() { value += Pyramid< N - 1 >().value + Row< N >().value; }
};
template<>
struct Pyramid<0> {
    std::string value;
};

int main()
{
    std::cout << Pyramid< 5 >().value;
    return 0;
}
Topic archived. No new replies allowed.