home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:16777 comp.std.c++:1610
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!xjzhu
- From: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
- Subject: Recursive Template? Are you sure?
- Message-ID: <By6KLy.1yB@math.uwaterloo.ca>
- Keywords: template, factorial
- Organization: University of Waterloo
- Date: Mon, 23 Nov 1992 17:58:45 GMT
- Lines: 44
-
- Hi, there:
-
- While everybody is so hot on the topic about "Recursive template",
- I can't seem to make the following code compile under AT&T C++ V3.0.
-
- Can comeone enlight me about this? (or maybe the original creator of
- this piece of code) It seems to me that you have to instantiate every
- class which gets involed in. For example, in this case, you have to
- instantiate class Factorial<1>, class Factorial<2>, ..., class
- Factorial<5>, but if this is the case, what's the so good about
- this recursive template? I don't get the point.
-
- *********Code Segment follows************
-
- #include <iostream.h>
-
- template<int n> class Factorial
- {
- public:
- int eval() { return n*Factorial<n-1>.eval(); }
- };
-
- class Factorial<0>
- {
- public:
- int eval() { return 1; }
- };
-
- int main()
- {
- Factorial<6> f6;
- cout << f6.eval() << endl;
- return 0;
- }
- ********** Code Segment Ends **********
-
-
- --------------------------------------------------------------
- A template version of signature class is under repair.
- Symptom: It dies without a warning if I use certain class
- as an argument.
-
- xjzhu@math.uwaterloo.ca
- --------------------------------------------------------------
-