[Insight-users] Borland 6 Build error

John Biddiscombe John Biddiscombe" <jbiddiscombe@skippingmouse.co.uk
Sat, 23 Nov 2002 21:23:30 -0000


With #define ENUMWORKS I get

Factorial of 5 = 120 // I added the factorial code too
Region 5
Region 0
Region 0

but with #undef ENUMWORKS

Factorial of 5 = 120
Region 5
Region 4
Region 4

Which is interesting. It looks like enums are dodgy alright, but only when
you recurse at least once...

JB

---------------------code below

#include <iostream>

#define ENUMWORKS
#undef ENUMWORKS

template<int N>
class Factorial {
public:
#ifndef ENUMWORKS
     static const unsigned int value = N * Factorial<N-1>::value;
#else
     enum { value = N * Factorial<N-1>::value };
#endif
     Factorial()
     {
       std::cout << "Factorial of " << N << " = " << value << std::endl;
     }
};

class Factorial<1> {
public:
#ifndef ENUMWORKS
     static const unsigned int value = 1;
#else
     enum { value = 1 };
#endif
};

template<int N>
class ImageRegion {
public:

     typedef ImageRegion<N> Self;
#ifndef ENUMWORKS
     static const unsigned int  Dimension = N;
     static const unsigned int  SliceDimension = N-1;
     typedef ImageRegion< N-1 > SliceRegion;
#else
     enum { Dimension = N };
     enum { SliceDimension = N-1 };
     typedef ImageRegion< SliceDimension > SliceRegion;
#endif
     SliceRegion Slice() { return SliceRegion(); }
     ImageRegion() { std::cout << "Region " << N << std::endl; }
};

#pragma argsused
main()
{
   Factorial<5> f;

   typedef ImageRegion<5> RegionType;
   RegionType region;
   RegionType::SliceRegion sliceRegion;
   sliceRegion = region.Slice();

   char ch;
   std::cin >> ch;

   return 0;
}