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

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!hhc
  3. From: hhc@athena.mit.edu (Hong-Hsiang Chen)
  4. Subject: Re: Recursive templates
  5. Message-ID: <1992Nov17.151111.12099@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: e40-008-4.mit.edu
  8. Organization: Massachusetts Institute of Technology
  9. References: <83733@ut-emx.uucp> <1992Nov16.220526.21566@borland.com>
  10. Date: Tue, 17 Nov 1992 15:11:11 GMT
  11. Lines: 60
  12.  
  13. In article <1992Nov16.220526.21566@borland.com> pete@borland.com (Pete Becker) writes:
  14. >In article <83733@ut-emx.uucp> jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
  15. >>Has ANSI decided anything about the expansion of a recursive template
  16. >>definition?
  17. >
  18. Interesting...I tried running the following example with gcc 2.3.1...
  19. >    template <unsigned n> class Factorial
  20. >    {
  21. >    public:
  22. >        unsigned eval() { return n*Factorial<n-1>.eval(); }
  23.                                            ^^^^^^^^^^^^^^^^^^^^^
  24.                                            what?? there's no instance here!
  25. I used:
  26.                 static unsigned eval() { return n*(Factorial<n-1>::eval()); }
  27. >    };
  28. >
  29. >    class Factorial<0>
  30. >    {
  31. >    public:
  32. >        unsigned eval() { return 1; }
  33. again, I used
  34.                 static unsigned eval() { return 1; } 
  35. >    };
  36. >
  37. >    #include <iostream.h>
  38. >
  39. >    int main()
  40. >    {
  41. >    Factorial<6> f6;
  42. >    cout << f6.eval() << endl;
  43. I used
  44.         cout << (Factorial<6>::eval()) << endl;
  45. >    return 0;
  46. >    }
  47. >
  48. >    That's from memory, so I may have some of the details wrong, but it
  49. >worked in BC 3.0, and from discussions I've had the same sort of thing works
  50. >with cfront.
  51. Well, it seems that it would make more sense to have no instantations of the
  52. class at all...
  53. >    -- Pete
  54. Interesting that it actually works with gcc 2.3.1...impressive...
  55.  
  56. Come to think of it, there shouldn't be any reason why recursive templates 
  57. shouldn't work...anybody have an argument against it?  (besides it being a
  58. vehicle for more obsfucated code :-))
  59.  
  60. Curiously, gcc 2.3.1 gives me a warning about shadowing member 'eval' with
  61. member function... I guess I should use virtuals, eh?
  62.  
  63. Well, more thoughts on this later...
  64.  
  65.  
  66.  
  67.  
  68. --
  69. -----
  70. Sean Chen.  hhc@athena.mit.edu
  71. "Nunc in scutella iaceo,/et volitare nequeo,/dentes fredentes video./Miser!"
  72.   Olim lacus colueram, _Carmina Burana_
  73.