From: mrs@cygnus.com (Mike Stump)
Subject: Re:  const member function in template definition is backwards
Organization: GNUs Not Usenet
Distribution: gnu
Date: Tue, 10 Nov 1992 21:11:44 GMT

This problem has been fixed, and should not exist in the release after
next of gcc.

However, one does get the following:

t5.cc: In function `int  main ()':
t5.cc:19: template for method `peekc' doesn't match any in class `Gorf<double>'
t5.cc:22: non-const member function `double  Gorf<double>::peek ()'
t5.cc:44: called for const object at this point in file
t5.cc:24: non-const member function `void  Gorf<double>::poke (double)'
t5.cc:45: called for const object at this point in file
t5.cc:17: non-const member function `double  Gorf<double>::peekc ()'
t5.cc:46: called for const object at this point in file
t5.cc: In method `void  Gorf<double>::pokec (double)const ':
t5.cc:31: warning: assignment of read-only member `data'

Hope these are ok...

> Message-Id: <9209182110.AA20905@malibu.pic.ucla.edu>
> From: Jim Carter <jimc@pic.ucla.edu>
> To: bug-g++@prep.ai.mit.edu
> Date: Fri, 18 Sep 92 14:10:54 -0700

> // BUG5.C -- const member function in template definition is backwards
> // gcc version 2.2.2 for sun-sparc-sunos411
> // Command line: g++ bug5.c

> // When a member function is const, and is defined in a separate template
> // (with the const qualifier), it is said not to match its method declaration
> // in the class.  If the class declaration mistakenly lacks "const", though,
> // the template matches.  It is as if the template's "const" were being
> // ignored or reversed.  

> /*
> James F. Carter        (213) 825-2897
> UCLA-Mathnet;  6221 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA  90024-1555
> Internet: jimc@math.ucla.edu            BITNET: jimc%math.ucla.edu@INTERBIT
> UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc


> test.cc: In function `int  main ()':
> test.cc:20: template for method `pokec' doesn't match any in class `Gorf<double>'
> test.cc:22: non-const member function `double  Gorf<double>::peek ()'
> test.cc:38: called for const object at this point in file
> test.cc:24: non-const member function `void  Gorf<double>::poke (double)'
> test.cc:39: called for const object at this point in file
> test.cc:26: non-const member function `double  Gorf<double>::peekc ()'
> test.cc:40: called for const object at this point in file
>  */

> /* 	When not commented out, these inline definitions work as expected
> template <class T> struct Gorf {
>     T data;
>     Gorf(T d) : data(d) { }

>     T peek() { return data; }
>     void poke(T d) { data = d; }
>     T peekc() const { return data; }
>     void pokec(T d) const { data = d; }	// Assignment of readonly member "data"
> };		*/

> template <class T> struct Gorf {
>     T data;
>     Gorf(T d) : data(d) { }

>     T peek();
>     void poke(T d);
>     T peekc() /* const */;	// Left out as a test
>     void pokec(T d) const;
> };

> template <class T>
>     T Gorf<T>::peek() { return data; }
> template <class T>
>     void Gorf<T>::poke(T d) { data = d; }

> template <class T>
>     T Gorf<T>::peekc() const { return data; }
> 	// *** Template should be rejected but is not.

> template <class T>
>     void Gorf<T>::pokec(T d) const { data = d; }
> 	// Assignment of readonly member "data" (correct)
> 	// *** template for method `pokec' doesn't match any 
> 	// in class `Gorf<double>' (BOGUS)

> int main () {
>     Gorf<double> g(0.0);
>     g.peek();			// No complaints on these 4 calls
>     g.poke(1.0);
>     g.peekc();
>     g.pokec(2.0);

>     const Gorf<double> c(0.0);
>     c.peek();			// Non-const mbr fcn for const object (correct)
>     c.poke(1.0);		// Non-const mbr fcn for const object (correct)
>     c.peekc();			// Non-const mbr fcn due to
> 				// mismatched template test
>     c.pokec(2.0);		// No error message (correct)
> }

Newsgroups: gnu.g++.bug