home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13113 < prev    next >
Encoding:
Text File  |  1992-08-31  |  1.7 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!Xenon.Stanford.EDU!ralph
  3. From: ralph@cs.stanford.edu (Ralph Melton)
  4. Subject: Templates, const member functions, and g++
  5. Message-ID: <ralph.715293911@Xenon.Stanford.EDU>
  6. Summary: When I try to use const member functions in class templates, g++ complains
  7. Originator: ralph@Xenon.Stanford.EDU
  8. Keywords: templates, const, g++
  9. Sender: news@CSD-NewsHost.Stanford.EDU
  10. Organization: CS Department, Stanford University, California, USA
  11. Date: 31 Aug 92 20:45:11 GMT
  12. Lines: 49
  13.  
  14. The following program, abstracted from larger work, makes g++ 2.2.2 complain
  15. "test.C:9:'template for method `bar' doesn't match any in class 'foo<2,3>'".
  16.  
  17. I need the operator int() to be a constant member function.  Removing
  18. both instances of the word 'const' lets g++ compile this correctly,
  19. but that gives me other problems in the rest of my program.
  20.  
  21. I don't completely understand the ramifications of templates and of
  22. the const keyword.  I would appreciate it if someone would tell me
  23. either a) "this is the correct procedure for declaring a constant
  24. member of a template class, and you should file a bug report for
  25. g++.", or b) "No, the way you declare a constant member function of a
  26. template class is this."
  27.  
  28. Thanks in advance,
  29.  
  30. Ralph Melton
  31.  
  32.  
  33. //--- cut here
  34. #include <ostream.h>
  35.  
  36. template <int lb, int ub>
  37. class foo
  38. {
  39.   int f;
  40. public:
  41.   operator int() const;
  42. };  // line 9.
  43.  
  44.  
  45. template <int lb, int ub>
  46. foo<lb,ub>::operator int() const
  47. {
  48.   return f;
  49. }
  50.  
  51. main()
  52. {
  53.   foo<2,3> the_foo;
  54.   cout << int(the_foo);
  55. }
  56. // end of file.
  57.  
  58.  
  59.  
  60. -- 
  61. Ralph Melton            ralph@cs.stanford.edu
  62. "Then ye shall know the truth, and the truth shall make you free."
  63.