Language C++
(meta programming)
| Date: | 04/20/05 |
| Author: | Arion Lei |
| URL: | n/a |
| Comments: | 2 |
| Info: | n/a |
| Score: |
// 99 bottles of beer, C++ template 'meta-programming' version
// By Arion Lei (philipl@cs.ust.hk)
#include <iostream.h>
template<int I>
class Loop {
public:
static inline void f () {
cout << I << " bottles of beer on the wall," << endl
<< I << " bottles of beer." << endl
<< "Take one down, pass it around," << endl
<< I-1 << " bottles of beer on the wall." << endl;
Loop<I-1>::f();
}
};
class Loop<0> {
public:
static inline void f () {
cout << "Go to the store and buy some more," << endl
<< "99 bottles of beer on the wall." << endl;
}
};
int main () {
Loop<3>::f();
return 0;
}
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| extreme template metaprogramming | Richard Wolf | 04/20/05 | 5 | |
| hacking style | Tim Robinson | 04/20/05 | 6 | |
| ob fuscated | Tapi (Paddy O'Brien) | 08/11/05 | 2 | |
| object-oriented version | Tim Robinson | 04/20/05 | 2 | |
| Preprocessor & self-include recursion | Chocapic | 02/27/07 | 0 | |
| GUI version | Martyn Davies | 05/28/05 | 0 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
shouldn't loop<3> be loop<99> in main()?
of beer,C++
template -'
meta-progr-
amming'ver.
By:Arion-Lei
(philipl@cs
.ust.hk)*/
#include <
iostream.h
>template<
int I>class
Loop{privet:
public: static
inline void f ()
{cout << I << " b"
<<"ottles of beer"<<
" on the wall,"<<endl
<< I << " bottles of"
<<"beer."<< endl<< "T\
\ake one down, pass i"
<< "t around," << endl
<< I-1 <<" bottles of"
<<" beer on the wall."
<<endl<<'\b'<<endl<<""
;Loop<I+(-1)>::f();}};
class Loop<0> {public:
static inline void f()
{cout << "Go to the s"
<<"tore and buy some";
cout<<" more," << endl
<<"99 bottles of beer\
\on the wall." << endl;
}};int main (){Loop<3>
::f( ); return (0) ;}