home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!Xenon.Stanford.EDU!ralph
- From: ralph@cs.stanford.edu (Ralph Melton)
- Subject: Templates, const member functions, and g++
- Message-ID: <ralph.715293911@Xenon.Stanford.EDU>
- Summary: When I try to use const member functions in class templates, g++ complains
- Originator: ralph@Xenon.Stanford.EDU
- Keywords: templates, const, g++
- Sender: news@CSD-NewsHost.Stanford.EDU
- Organization: CS Department, Stanford University, California, USA
- Date: 31 Aug 92 20:45:11 GMT
- Lines: 49
-
- The following program, abstracted from larger work, makes g++ 2.2.2 complain
- "test.C:9:'template for method `bar' doesn't match any in class 'foo<2,3>'".
-
- I need the operator int() to be a constant member function. Removing
- both instances of the word 'const' lets g++ compile this correctly,
- but that gives me other problems in the rest of my program.
-
- I don't completely understand the ramifications of templates and of
- the const keyword. I would appreciate it if someone would tell me
- either a) "this is the correct procedure for declaring a constant
- member of a template class, and you should file a bug report for
- g++.", or b) "No, the way you declare a constant member function of a
- template class is this."
-
- Thanks in advance,
-
- Ralph Melton
-
-
- //--- cut here
- #include <ostream.h>
-
- template <int lb, int ub>
- class foo
- {
- int f;
- public:
- operator int() const;
- }; // line 9.
-
-
- template <int lb, int ub>
- foo<lb,ub>::operator int() const
- {
- return f;
- }
-
- main()
- {
- foo<2,3> the_foo;
- cout << int(the_foo);
- }
- // end of file.
-
-
-
- --
- Ralph Melton ralph@cs.stanford.edu
- "Then ye shall know the truth, and the truth shall make you free."
-