Language C++
(meta programming)
Date: | 04/20/05 |
Author: | Arion Lei |
URL: | n/a |
Comments: | 5 |
Info: | n/a |
Score: | (2.96 in 24 votes) |
// 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 |
---|---|---|---|---|
object-oriented version | Tim Robinson | 04/20/05 | 4 | |
hacking style | Tim Robinson | 04/20/05 | 14 | |
extreme template metaprogramming | Richard Wolf | 04/20/05 | 5 | |
Preprocessor & self-include recursion | Chocapic | 02/27/07 | 6 | |
feature creep | Jono | 04/27/09 | 1 | |
most extreme template metaprogramming | Henrik Theiling | 12/04/11 | 0 | |
7 | Jono | 04/15/09 | 0 | |
ob fuscated | Tapi (Paddy O'Brien) | 08/11/05 | 4 | |
GUI version | Martyn Davies | 05/28/05 | 1 |
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
Bob said on 06/24/05 21:07:03
Nice use of tmpl specialization and all, but
shouldn't loop<3> be loop<99> in main()?
saarya said on 04/05/06 14:36:51
a bit of formmating: /*99bottles
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) ;}
9+6VD said on 08/10/08 00:50:15
nize
Dysnomia said on 08/13/08 00:09:46
Don't you have to add template <> before the explicate specialization on line 18?
I think it should read
template <> class Loop<0> {
I haven't tried compiling it your way but it caught my eye.
-Dys
Lurveleven said on 05/31/10 11:42:59
Not even near of giving the correct output.