home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16777 < prev    next >
Encoding:
Text File  |  1992-11-23  |  1.6 KB  |  56 lines

  1. Xref: sparky comp.lang.c++:16777 comp.std.c++:1610
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!xjzhu
  4. From: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
  5. Subject: Recursive Template? Are you sure?
  6. Message-ID: <By6KLy.1yB@math.uwaterloo.ca>
  7. Keywords: template, factorial
  8. Organization: University of Waterloo
  9. Date: Mon, 23 Nov 1992 17:58:45 GMT
  10. Lines: 44
  11.  
  12. Hi, there:
  13.  
  14. While everybody is so hot on the topic about "Recursive template",
  15. I can't seem to make the following code compile under AT&T C++ V3.0.
  16.  
  17. Can comeone enlight me about this? (or maybe the original creator of 
  18. this piece of code) It seems to me that you have to instantiate every
  19. class which gets involed in. For example, in this case, you have to 
  20. instantiate class Factorial<1>, class Factorial<2>, ..., class
  21. Factorial<5>, but if this is the case, what's the so good about
  22. this recursive template? I don't get the point.
  23.  
  24. *********Code Segment follows************
  25.  
  26. #include <iostream.h>
  27.  
  28. template<int n> class Factorial
  29. {
  30. public:
  31.     int eval() { return n*Factorial<n-1>.eval(); }
  32. };
  33.  
  34. class Factorial<0>
  35. {
  36. public:
  37.     int eval() { return 1; }
  38. };
  39.  
  40. int main()
  41. {
  42.     Factorial<6> f6;
  43.     cout << f6.eval() << endl;
  44.     return 0;
  45. }
  46. ********** Code Segment Ends **********
  47.  
  48.  
  49. --------------------------------------------------------------
  50.    A template version of signature class is under repair.
  51. Symptom: It dies without a warning if I use certain class 
  52.          as an argument.
  53.          
  54.                                        xjzhu@math.uwaterloo.ca
  55. --------------------------------------------------------------
  56.