public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* New c++ bug with conversion operators?
@ 1998-05-06 16:13 Benjamin Redelings I
  1998-05-08 19:52 ` Martin von Loewis
  1998-05-25 13:43 ` PATCH for " Mark Mitchell
  0 siblings, 2 replies; 3+ messages in thread
From: Benjamin Redelings I @ 1998-05-06 16:13 UTC (permalink / raw)
  To: egcs

Hi!, I recently went from the 980315 snapshot to 19980502.  gcc still
compiles the kernel OK with -mpentiumpro -O12 -fstrength-reduce, but I
have a new problem with one of my C++ projects.

g++ now appears not to want 'const' conversion operators any more...
Here is my sample code:
---------------test.C------------------
struct string
{
  char* location;
  int length;

  operator const char*() const{return location;}
  /*  operator const char*() {return location;} */
  operator char*() {return location;}
};

main() {
  int i;
  string s1;
  const string s2=s1;

  i=strlen(s1);
  i=strlen(s2);
}
--------------end test.C--------------
Here is the text of the warnings:
telomere:~/devel/Psh.0.4.6.8> make test
g++ -fno-rtti -fno-exceptions -Wall -g -O13 -DNDEBUG -mpentiumpro
-I../include -fno-rtti -fno-exceptions   test.C   -o test
test.C: In function `int main()':
test.C:16: warning: choosing `string::operator char *()' over
`string::operator const char *() const'
test.C:16: warning:   for conversion from `string' to `const char *'
test.C:16: warning:   because conversion sequence for `this' argument is
better
test.C:16: warning: choosing `string::operator char *()' over
`string::operator const char *() const'
test.C:16: warning:   for conversion from `string' to `const char *'
test.C:16: warning:   because conversion sequence for `this' argument is
better


	This seems rather strange to me...the second 'const' in the declaration
of
'operator const char*()' just means that "*this" will not be changed
during the conversion (nor will be changed afterwards, with the info
provided by the conversion).  But g++ is saying that it won't use this
operator on 's1' because 's1' wasn't declared const!  That is
irrelevant.
	If it is possible to consider the type that s1 is being cast to by
strlen, then it should consider that instead and convert to 'const
char*'.

If you uncomment the second operator definition, the warning goes away. 
Wierd.  Am I perhaps coding this in the wrong way?  That DOES seem likea
strange way to code, but AFAIK C++ requires it.

-BenRI

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-05-25 13:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-05-06 16:13 New c++ bug with conversion operators? Benjamin Redelings I
1998-05-08 19:52 ` Martin von Loewis
1998-05-25 13:43 ` PATCH for " Mark Mitchell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).