This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C++ compiler bug ?



Hello,

I think the following is a bug in the C++ compiler:

------------------------------------------------------

$> cat t2.cxx


class string {

   char *s;
public:

   string(const char *c) {s=c;}

   operator char *() { return s; }
   operator const char *() const { return s; }
};

void foo(const char *s)
{
}

main()
{
   string a("test");

   foo(a);

   return 0;
}

$> gcc t2.cxx
t2.cxx: In method `string::string(const char *)':
t2.cxx:8: assignment to `char *' from `const char *' discards qualifiers
t2.cxx: In function `int main()':
t2.cxx:22: warning: choosing `string::operator char *()' over
`string::operator const char *() const'
t2.cxx:22: warning:   for conversion from `string' to `const char *'
t2.cxx:22: warning:   because conversion sequence for the argument is better

$> gcc --version
2.95.2

--------------------------------------------------------------------

I have checked the final draft of the ANSI C++ standard (section 13.3.3.2)
and I think, since opterator const char *() is an exact match, I think this 
conversion operator should be used instead of operator char *().

Conversion sequences:

1) string->const char *
2) string->char *->const char *

gcc selects 2 over 1.

Regards,


	Matthias Meixner

-- 
Matthias Meixner                   meixner@rbg.informatik.tu-darmstadt.de
Technische Universität Darmstadt
Rechnerbetriebsgruppe                          Telefon (+49) 6151 16 6670
Wilhelminenstraße 7, D-64283 Darmstadt, Germany    Fax (+49) 6151 16 4701

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]