nested class in recursive template

Stefan Schwarzer sts@ica1.uni-stuttgart.de
Mon May 4 14:22:00 GMT 1998


// Dear cygnus/gnu/fsf development team,
//
// I thought, just in case you are interested:
//
// I can't get the appended code to compile with egcs:
//    gcc version egcs-2.91.25 19980425 (gcc2 ss-980401 experimental)
//    gcc version egcs-2.90.28 980423 (egcs-1.0.3 prerelease)
//      on solaris 2.5
//    gcc version egcs-2.90.28 980423 (egcs-1.0.3 prerelease)
//      on Digital Unix 4.0C

//22:25 boa_sts:~/bugs/gcc> g++ iarr.cc
//iarr.cc:47: incorrect number of parameters (1, should be 2)
//iarr.cc:5: in template expansion for decl `template <class T, int const N> Add<T,N>'
//iarr.cc:47: confused by earlier errors, bailing out

// If I leave out the 'typename' the compiler finds a syntax error (compiles
//   fine with KCC 3.3 or cxx 6.1) -- is typename really necessary here, 
//   since the compiler should see that Sum is a nested class? But fine. 
//
// A similar code, where Sum is not a nested non-template, 
//   but a regular 'global' template class Sum<T,int>, works fine (this is my 
//   workaround).
//
// The problem also disappears with only one template arg 
//   (just the int, T=double).
//
// Best regards - Stefan. 
//
// ps: Sorry, I am really bad in making compliments, but I think that you are
//   far along the way to a really good compiler, it's just my bizarre 
//   thinking and naiive programming that always get's me into trouble...  
//
//Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658 
//Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
//Pfaffenwaldring 27              pgp public key available on request/finger 
//70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts


#include "iostream.h"

template <class T, int N>
class Add {
public:
  Add(){}

  class Sum;
  typename Add<T,N-1>::Sum add(T n) const
    //Add<T,N-1>::Sum add(T n) const
  { return Add<T,N-1>::Sum(n); }
};

template <class T, int N> 
class Add<T,N>::Sum {
public:
  Sum(T n){ sum = n; }
  typename Add<T,N-1>::Sum add(T n) const
    //Add<T,N-1>::Sum add(T n) const
  { return Add<T,N-1>::Sum(sum + n); }
  T sum;
};


template<class T>
class Add<T,1> { 
public:
  Add<T,1>(){}
  class Sum;
  T add(T n) const
    { return n; }
};  

template<class T>
class Add<T,1>::Sum {
public:
  Sum(int n){ sum = n; };
  T add(T n) const { return sum + n; }
  T sum;
};


main()
{
  //cout << Add<double,1>().add(21.) << "\n";  
  cout << Add<double,2>().add(21.).add(2.) << "\n";
}
  





More information about the Gcc-bugs mailing list