home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19734 < prev    next >
Encoding:
Text File  |  1993-01-22  |  1.8 KB  |  85 lines

  1. Path: sparky!uunet!mcsun!ub4b!alcbel!ta7s49.alcbel.be
  2. From: ldup@ta7s49.alcbel.be (Luc Duponcheel)
  3. Newsgroups: comp.lang.c++
  4. Subject: templates and friends (again)
  5. Message-ID: <1949@alcbel.be>
  6. Date: 21 Jan 93 08:14:28 GMT
  7. Sender: ldup@alcbel.be
  8. Organization: Alcatel Bell Telephone, Antwerpen, Belgium
  9. Lines: 74
  10.  
  11. Sorry, there were some little typing errors in my first mail (cut/paste ...).
  12. The (ugly) solutions were, as you might have guessed, template functions with
  13. a type template argument omly. So I repeat the mail .
  14.  
  15.  
  16. --------start--------------
  17.  
  18.  
  19. I have some question about templates and friends.
  20.  
  21. Since friend templates are function templates, they cannot have non-type 
  22. template arguments.
  23.  
  24.  
  25. Therefore the following example will not compile
  26.  
  27. ---------begin------------
  28.  
  29. #include <iostream.h>
  30.  
  31. template<class T, int size> 
  32. class Container {
  33. friend ostream &operator<<(ostream &, const Container<T,size> &) ;
  34. } ;
  35.  
  36. template<classT, int size>
  37. ostream &operator<<(ostream &os, const Container<T,size> &c) {
  38.  // do something
  39.  return os ;
  40.  
  41. ----------end-------------
  42.  
  43. The following example compiles
  44.  
  45. ---------begin------------
  46.  
  47. #include <iostream.h>
  48.  
  49. template<class T, int size> 
  50. class Container {
  51. friend ostream &operator<<(ostream &, const Container<T,5> &) ;
  52. friend ostream &operator<<(ostream &, const Container<T,10> &) ;
  53. // ...
  54. } ;
  55.  
  56. template<classT>
  57. ostream &operator<<(ostream &os, const Container<T,5> &c) {
  58.  // do something
  59.  return os ;
  60.  
  61. template<classT>
  62. ostream &operator<<(ostream &os, const Container<T,10> &c) {
  63.  // do something
  64.  return os ;
  65.  
  66. // ...
  67.  
  68. ----------end-------------
  69.  
  70. I do NOT think this is the way to solve the problem.
  71.  
  72. Are there any better solutions?
  73.  
  74. P.S.
  75.  
  76. What is the very reason for not allowing non-type template arguments 
  77. for function templates?
  78.  
  79.  
  80.  
  81.  
  82.