[PATCH] Fix PR c++/26698: g++ accepts const-incorrect code due to conversion function

Simon Martin simartin@users.sourceforge.net
Sun Oct 7 19:49:00 GMT 2007


Hi all.

This PR was opened because we would not handle the following snippet
properly:

=== cut here ===
struct X {
        int x;
        X ( int i = 0 )
          : x ( i )
        {}
        operator X & ( void ) const {
          return ( *this );
        }
};
void add_one ( X & ref ) {
        ++ ref.x;
}
int main ( void ) {
        X const a ( 2 );
        add_one( a );
}
=== cut here ===

At the time this PR was opened, we would accept it without complaining,
while there are two problems:
       1. The body of "operator X&" is invalid because it returns a
non-const reference to the const object it's called on.
       2. This operator should not be considered when calling add_one,
because the standard states that "A conversion function is never used to
convert a (possibly cv-qualified) object to the (possibly cv-qualified)
same object type (or a reference to it), to a (possibly cv-qualified)
base class of that type (or a reference to it)".

The first point is now properly rejected, but the second one is still
present.

The attached patch modifies build_user_type_conversion_1 so that
conversion operators are not considered when they should not according
to the standard.

I have successfully regtested it on i386-apple-darwin8.10.1. Is it OK
for the mainline? For 4.2 after 4.2.2 is released?

Best regards,
Simon

:ADDPATCH c++:





-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CL_26698
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20071007/48845ee1/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pr26698.patch
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20071007/48845ee1/attachment-0001.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CL_26698_testsuite
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20071007/48845ee1/attachment-0002.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: op4.C
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20071007/48845ee1/attachment-0003.ksh>


More information about the Gcc-patches mailing list