Skip to content

Instantly share code, notes, and snippets.

@mpapierski
Created May 18, 2013 23:33
Show Gist options
  • Save mpapierski/5606088 to your computer and use it in GitHub Desktop.
Save mpapierski/5606088 to your computer and use it in GitHub Desktop.
having fun with C++ metaprogramming and 99 bottles of beer.
#include <cstdlib>
#include <cstdio>
template <int N>
struct bottles;
template <>
struct bottles<100>
{
};
template <int N>
struct bottles: bottles<N + 1>
{
bottles()
{
std::printf("%d bottles of beer on the wall, %d bottles of beer.\n", N, N);
std::printf("Take one down and pass it around, %d bottles of beer on the wall.\n", N - 1);
std::printf("\n");
}
};
template <>
struct bottles<1>: bottles<2>
{
bottles()
{
std::printf("1 bottle of beer on the wall, 1 bottle of beer.\n");
std::printf("Take one down and pass it around, no more bottles of beer on the wall.\n");
std::printf("\n");
}
};
template <>
struct bottles<0>: bottles<1>
{
bottles()
{
std::printf("No more bottles of beer on the wall, no more bottles of beer.\n");
std::printf("Go to the store and buy some more, 99 bottles of beer on the wall.\n");
std::printf("\n");
}
};
struct _99_bottles_of_beer: bottles<0> {};
int
main(int argc, char * argv[])
{
_99_bottles_of_beer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment