From mboxrd@z Thu Jan 1 00:00:00 1970 From: Benjamin Redelings I To: egcs@cygnus.com Subject: New c++ bug with conversion operators? Date: Wed, 06 May 1998 16:13:00 -0000 Message-id: <3550C34B.B5D0BAA3@ucsd.edu> X-SW-Source: 1998-05/msg00210.html 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