Syntax bug

Mark Mitchell mmitchell@usa.net
Tue Feb 10 11:39:00 GMT 1998


>>>>> "Pavel" == Pavel A Kalugin <kalugin@pc358.lps.u-psud.fr> writes:

    Pavel> Here's the code snippet revealing the bug in the C++
    Pavel> parser:

    Pavel> ////////////////////////////////////////////////////////////
    Pavel> class X { public: X() : data(new char[10]){} ~X() {
    Pavel> delete[] data; } operator const char* () const {return
    Pavel> data;} operator char* () {return data;} private: char*
    Pavel> data; };

    Pavel> void foo() { X x; x[0]='q'; }
    Pavel> ///////////////////////////////////////////////////////////////

    Pavel> I got the following when compiling it with egsc:

    Pavel> x.cpp: In function `void foo()': x.cpp:17: ambiguous
    Pavel> conversion for array subscript

That's a correct error message.  The compiler doesn't know whether to
use the const char* conversion or the char* conversion.

    Pavel> On the other hand, an _incorrect_ declaration
    Pavel> const operator char* () const {return data;}
    Pavel> makes the compiler quite happy. This bug existed in
    Pavel> gcc-2.7.2.3 as well.

That's because the compiler just ignores the first const.  So, it's
just like you wrote

  operator char* () const;

Here, both operators convert to char* so it picks the one that is not
const-qualified as (in your example) x is not const.  I'm about to
submit a patch that will make the illegal:

  const operator char* () const;

declaration provoke an error message.

-- 
Mark Mitchell		mmitchell@usa.net
Stanford University	http://www.stanford.edu




More information about the Gcc-bugs mailing list