home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / std / cplus / 1087 < prev    next >
Encoding:
Text File  |  1992-08-17  |  1.8 KB  |  61 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!caen!sdd.hp.com!apollo.hp.com!netnews
  3. From: vinoski@apollo.hp.com (Stephen Vinoski)
  4. Subject: const and template parameters
  5. Originator: vinoski@zep_r.ch.apollo.hp.com
  6. Sender: usenet@apollo.hp.com (Usenet News)
  7. Message-ID: <Bt5ECn.CtF@apollo.hp.com>
  8. Date: Mon, 17 Aug 1992 21:57:58 GMT
  9. Nntp-Posting-Host: zep_e.ch.apollo.hp.com
  10. Organization: Hewlett-Packard Company, Apollo Division - Chelmsford, MA
  11. Lines: 48
  12.  
  13.  
  14. Recently I wondered about the use of const for the arguments of class
  15. template member functions and function templates.  For example, given
  16. this function template:
  17.  
  18.     template<class T>
  19.     void
  20.     tfunc(const T arg) { // ... }
  21.  
  22. what happens when the type ``int *'' is used for the formal template
  23. parameter ``T''?
  24.  
  25. Mentally, one might make the parameter substitution and come up with
  26. the following equivalent function:
  27.  
  28.     void tfunc(const int *arg);
  29.  
  30. Under this interpretation, the argument appears to be a pointer to
  31. const int.
  32.  
  33. What actually should happen, though, is that const should apply to
  34. ``T'', so that the type parameter substitution results in
  35.  
  36.     void tfunc(int *const arg);
  37.  
  38. with the argument actually being a const pointer to int.
  39.  
  40. The latter interpretation is exactly the same as the "const applied to
  41. typedef" problem:
  42.  
  43.     typedef int *IntPtr;
  44.     const IntPtr int_ptr;
  45.  
  46. Here, int_ptr is a const pointer to int (sometimes much to the
  47. amazement of C++ novices :-)).
  48.  
  49. It appears that the use of const under these circumstances is somewhat
  50. misleading, and perhaps buys us very little.  Should developers leave
  51. const out of these areas of function and class templates?  Your
  52. thoughts?
  53.  
  54. thanks,
  55. -steve
  56.  
  57. -- 
  58. Steve Vinoski  (508)436-5904   vinoski@apollo.hp.com
  59. Distributed Object Computing Program
  60. Hewlett-Packard, Chelmsford, MA 01824       These are my opinions.
  61.