2000

Grusel++ of the month

Home
Download Source
/******************************************************************************/
/*                                                                            */
/*                                                              FILE: MAY.CPP */
/*                                                                            */
/*  Did you know that C++ even supports compile time recursion ?              */
/*  ============================================================              */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   31-MAY-2000   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/******************************************************************************/

#include <iostream.h>

template<const int n>
class Count
{
 public:
   static inline print()
   {
      n > 0 && Count< n - 1 >::print() ;

      cout << n << " " ;

      return n ;
   }
} ;

int main( )
{
 return Count< 5 >::print() ;
}

Update 21. May 2020

This program needed more work. The compiler stopped after complaining about 900 recursive calls.

The condition is no longer evaluated as it was 20 years ago. This might be caused by a change in the specification, or by one of the compilers not correctly implementing the specification.

I resolved the problem by template specialization for the case of 0.

Minor problems were the old-style header and the missing using declaration. The newer compiler also complained that print needs a return type.

/******************************************************************************/
/*                                                                            */
/*                                                              FILE: MAY.CPP */
/*                                                                            */
/*  Did you know that C++ even supports compile time recursion ?              */
/*  ============================================================              */
/*                                                                            */
/*  Compiled and tested with Visual C++ V6.0                                  */
/*                                                                            */
/*  V1.00   31-MAY-2000   P. Tellenbach   http://www.heimetli.ch/             */
/*                                                                            */
/*  Compiled and tested with g++ V8.3.0                                       */
/*                                                                            */
/*  V2.00   21-MAY-2020   P. Tellenbach   https://www.heimetli.ch/            */
/*                                                                            */
/******************************************************************************/

#include <iostream>

using namespace std ;

template<const int n>
class Count
{
 public:
   static inline int print()
   {
      Count< n - 1 >::print() ;

      cout << n << " " ;

      return 0 ;
   }
} ;

template<>
class Count<0>
{
 public:
   static inline bool print()
   {
      cout << 0 << " " ;

      return 0 ;
   }
} ;

int main( )
{
 return Count<5>::print() ;
}