Listing 11

// Traditional metaloops to calculate the square root

template <int N, int low=1, int hi=N>
struct Sqrt
{
  enum { mid = (lo+ (hi-low)/2 };
  enum { result = If< (mid*mid >N), Sqrt<N,low,mid>, 
                                  Sqrt<N,mid+1,hi>
                  >::result } ;
};
template <int N, int low>
struct Sqrt<N, low, low> 
{
   enum {result = low};
};
cout <<  Sqrt<16>::result;   // print square root of 16