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

  1. Xref: sparky comp.lang.c++:16634 comp.std.c++:1604
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!xjzhu
  4. From: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
  5. Subject: "Invalid" argument(s) in template class declaration, how to realize?
  6. Message-ID: <BxznCM.K9o@math.uwaterloo.ca>
  7. Keywords: template, argument
  8. Organization: University of Waterloo
  9. Date: Fri, 20 Nov 1992 00:14:46 GMT
  10. Lines: 45
  11.  
  12. Q: When designing a template class, how to make sure that its argument
  13.    can only be certain classes or certain predefined types?
  14.  
  15. Another words, can we build a template class such that it only accepts 
  16. certain types as its arguments, while for others it will generate an
  17. error message such as:
  18.  
  19.    This type is not supported by this template.
  20.  
  21. if you attempt to use them as the argument.
  22.  
  23. To give you an example( hopefully you will think it's reasonable! )
  24.  
  25. template<class T>
  26. class Expect_Numerical_In_Nature
  27. {
  28.    T Nian;
  29. public:
  30.    // ...
  31. };
  32.  
  33. and suppose that we also have:
  34.  
  35.     class Not_Numerical_In_Nature;
  36.  
  37. then we will expect the following declaration:
  38.  
  39.     Expect_Numberical_In_Nature<Not_Numerical_In_Nature> Not_Valid;
  40.  
  41. generate an error message.
  42.  
  43. How do we provide such a mechanism?
  44.  
  45. P.S. While I am writing this down, I feel this is probably a hard question,
  46.   because, I think, the template of C++ doesn't provide such property?
  47. (Well, just guess, don't get offended if it actually does.)  How hard
  48. will be a work around? 
  49.  
  50. --------------------------------------------------------------
  51.    A template version of my signature class is under repair.
  52. Symptom: It dies without a warning if I use certain class 
  53.          as an argument.
  54.          
  55.                                        xjzhu@math.uwaterloo.ca
  56. --------------------------------------------------------------
  57.