Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C++

(meta programming)

Date:04/20/05
Author:Arion Lei
URL:n/a
Comments:2
Info:n/a
Score: (3.33 in 12 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

VersionAuthorDateCommentsRate
extreme template metaprogrammingRichard Wolf04/20/055
hacking styleTim Robinson04/20/056
ob fuscatedTapi (Paddy O'Brien)08/11/052
object-oriented versionTim Robinson04/20/052
Preprocessor & self-include recursionChocapic02/27/070
GUI versionMartyn Davies05/28/050

Comments

>>  Bob said on 06/24/05 21:07:03

Bob 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

saarya 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) ;}

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!

Name:

eMail:

URL:

Security Code:
  
Comment: