home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / cplus / 1571 < prev    next >
Encoding:
Text File  |  1992-11-15  |  1.8 KB  |  63 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!ut-emx!emx.cc.utexas.edu
  2. From: jamshid@emx.cc.utexas.edu (Jamshid Afshar)
  3. Newsgroups: comp.std.c++
  4. Subject: Recursive templates
  5. Message-ID: <83733@ut-emx.uucp>
  6. Date: 16 Nov 92 07:16:12 GMT
  7. Sender: jamshid@ut-emx.uucp
  8. Organization: The University of Texas at Austin; Austin, Texas
  9. Lines: 52
  10.  
  11. Has ANSI decided anything about the expansion of a recursive template
  12. definition?  Consider the following class template:
  13.  
  14.     template<unsigned N>
  15.     class A {
  16.        int d[N];
  17.     public:
  18.        A<N-1> operator--() const
  19.           { return A<N-1>(); }
  20.        A<N+1> operator++() const
  21.           { return A<N+1>(); }
  22.        int& operator[](unsigned i)
  23.           { return d[i]; }
  24.     };
  25.  
  26. I agree
  27.     A<0> a0;
  28. should be an error since it results in a 0 length array, but what about
  29.     A<1> a1;
  30. if operator--() is never called on it?  Actually, will the effects of
  31. even compiling code like this be defined, or could a compiler give an
  32. "out of memory" error or crash (btw, BC++ 3.1 gives an error)?  Would
  33. recursion-stopping specializations for A<1> and, say, A<8> make a
  34. difference.  If so would they have to be in any particular order?
  35.  
  36.     class A<1> {
  37.     public:
  38.        int d[1];
  39.     public:
  40.        A<2> operator++() const
  41.           { return A<2>(); }
  42.        int& operator[](unsigned i)
  43.           { return d[i]; }
  44.     };
  45.  
  46.     class A<8> {
  47.        int d[8];
  48.     public:
  49.        A<7> operator--() const
  50.           { return A<7>(); }
  51.        int& operator[](unsigned i)
  52.           { return d[i]; }
  53.     };
  54.  
  55. I'm hoping ANSI will allow recursive templates.  This would require
  56. that compilers not expand a template function or member function
  57. unless it is actually called.  I'm worried, though, that there are
  58. situations where this is difficult or even impossible.  Has anyone
  59. thought this through?
  60.  
  61. Thanks, Jamshid Afshar
  62. jamshid@emx.utexas.edu
  63.