home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / cplus / 1575 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1006 b   |  45 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!ukma!netsys!decwrl!borland.com!pete
  3. From: pete@borland.com (Pete Becker)
  4. Subject: Re: Recursive templates
  5. Message-ID: <1992Nov16.220526.21566@borland.com>
  6. Originator: pete@genghis.borland.com
  7. Sender: news@borland.com (News Admin)
  8. Organization: Borland International
  9. References: <83733@ut-emx.uucp>
  10. Date: Mon, 16 Nov 1992 22:05:26 GMT
  11. Lines: 32
  12.  
  13. In article <83733@ut-emx.uucp> jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
  14. >Has ANSI decided anything about the expansion of a recursive template
  15. >definition?
  16.  
  17.     template <unsigned n> class Factorial
  18.     {
  19.     public:
  20.         unsigned eval() { return n*Factorial<n-1>.eval(); }
  21.     };
  22.  
  23.     class Factorial<0>
  24.     {
  25.     public:
  26.         unsigned eval() { return 1; }
  27.     };
  28.  
  29.     #include <iostream.h>
  30.  
  31.     int main()
  32.     {
  33.     Factorial<6> f6;
  34.     cout << f6.eval() << endl;
  35.     return 0;
  36.     }
  37.  
  38.     That's from memory, so I may have some of the details wrong, but it
  39. worked in BC 3.0, and from discussions I've had the same sort of thing works
  40. with cfront.
  41.     -- Pete
  42.  
  43.  
  44.  
  45.