#include <stdio.h>

/* ---- 1. a constant of any integer type ---- */

template <char c, unsigned long n>
void line()
{
    for (unsigned long i = 0; i < n; i++)
        putchar(c);
    putchar('\n');
}

template <bool loud>
void say(const char *msg)
{
    printf(loud ? "%s!!!\n" : "%s\n", msg);
}

/* ---- 2. ... known at compile time ---- */

const int width = 4;
enum { height = 3 };

/* ---- 3. the address of a global variable: &var ---- */

int apples = 0;
int pears = 0;

template <int *counter>
struct Counted {
    Counted() { ++*counter; }
};

/* ---- 4. the address of a function: &f or just f ---- */

void to_stdout(const char *msg) { printf("stdout: %s\n", msg); }
void to_stderr(const char *msg) { fprintf(stderr, "stderr: %s\n", msg); }

template <void (*report)(const char *)>
void work()
{
    report("done");
}

/* ---- for 5 and 7 ---- */

int numbers[3] = { 10, 20, 30 };
int *apples_ptr = &apples;
char greeting[] = "hello";

template <char *name>
void hello()
{
    printf("%s\n", name);
}

int main()
{
    line<'-', 10>();                    /* 1: char and unsigned long */
    say<true>("one");                   /* 1: bool */
    say<false>("two");

    line<'=', width * height + 1>();    /* 2: an expression of constants */
    line<'.', sizeof(int)>();

    Counted<&apples> a1, a2;            /* 3: &var */
    Counted<&pears> p1;
    printf("%d %d\n", apples, pears);

    work<&to_stdout>();                 /* 4: &f */
    work<to_stderr>();                  /* 4: f */

    hello<greeting>();                  /* 7: what works instead */
#if BAD == 1
    int n = 10;
    line<'-', n>();                     /* 2: not known at compile time */
#elif BAD == 2
    Counted<&numbers[1]> bad;           /* 5: a complex address expression */
#elif BAD == 3
    Counted<apples_ptr> bad;            /* 5: the value of a pointer */
#elif BAD == 4
    int local = 0;
    Counted<&local> bad;                /* 6: a local variable */
#elif BAD == 5
    hello<"hello">();                   /* 7: a string literal */
#endif
    return 0;
}
