Bug in g++ 3.X – invalid operands of types `A&’ and `int’ to binary `operator!=’

I am not sure if there is a bug declaration for g++ 3.X (have not checked the doc yet). On the other hand, g++ 4.X, however, could compile the example code below silently. Anyway, seems for g++ 3.X, reference operator ‘&’ and address operator ‘&’ would confuse the compiler when they are used the same time in one expression. There is also a workaround there to bypass the complain from older compiler.

/home/daveti/Ctest: cat reference.cpp
#include <stdio.h>

typedef struct _A
{
        int a;
        int b;
}A;

int main( void)
{
        A x;
        A &y = x;

        if ( &(y.a) || 1)
        {
                printf("Sounds goodn");
        }

        return 0;
}
/home/daveti/Ctest: g++ -o reference reference.cpp
reference.cpp: In function `int main()’:
reference.cpp:14: invalid operands of types `A&’ and `int’ to binary `
   operator!=’
/home/daveti/Ctest: g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –enable-shared –enable-threads=posix –disable-checking –with-system-zlib –enable-__cxa_atexit –host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

/home/daveti/Ctest: cat reference2.cpp
#include <stdio.h>

typedef struct _A
{
        int a;
        int b;
}A;

int main( void)
{
        A x;
        A &y = x;
        int p = y.a;

        if ( &(p) || 1)
        {
                printf("Sounds goodn");
        }

        return 0;
}
/home/daveti/Ctest: g++ -o reference2 reference2.cpp
/home/daveti/Ctest: ./reference2
Sounds good
/home/daveti/Ctest: g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure –prefix=/usr –mandir=/usr/share/man –infodir=/usr/share/info –enable-shared –enable-threads=posix –disable-checking –with-system-zlib –enable-__cxa_atexit –host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming, Stuff about Compiler and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s