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:5
Info:n/a
Score: (2.95 in 21 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
object-oriented versionTim Robinson04/20/054
hacking styleTim Robinson04/20/0514
Preprocessor & self-include recursionChocapic02/27/076
feature creepJono04/27/091
most extreme template metaprogrammingHenrik Theiling12/04/110
7Jono04/15/090
ob fuscatedTapi (Paddy O'Brien)08/11/054
GUI versionMartyn Davies05/28/051

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

>>  9+6VD said on 08/10/08 00:50:15

9+6VD nize

>>  Dysnomia said on 08/13/08 00:09:46

Dysnomia 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

Lurveleven Not even near of giving the correct output.

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: