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]
Other format: [Raw text]

[Bug c++/47026] New: invalid temporary is being assigned to a const-reference


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47026

           Summary: invalid temporary is being assigned to a
                    const-reference
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: tmoschou@gmail.com


Created attachment 22830
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=22830
Prog which demonstrates bug. returns EXIT_FAILURE for g++, EXIT_SUCCESS for
MSVC++

The following program (also attached) highlights an obvious bug in g++. When
compiled in Microsoft Visual C++, the program executes as expected: âptr ==
aliasâ. But when compiled with g++: âptr != aliasâ. I believe that this is
because a temporary is wrongly created in the statement,

const int* const &alias(prt);
or alternatively
const int* const &alias = ptr;

A âconst int*â temp is created from âptrâ of type âint*â. So âaliasâ is a
reference to the temp and not âptrâ. Iâve tried using a âconst_castâ operator
but with no luck for the desired results.

Program was compiled with the command: g++ -o prog.exe prog.cpp

program: prog.cpp
---------------<start>--------------
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {

    int a = 10;
    int* ptr = &a;

    /* temporary is being assigned to a const-reference : BAD */
    const int* const &alias(ptr);

    ptr = NULL; /* or ptr = 0; */

    if (ptr != alias) { 
        /* should never execute but DOES on g++ (not on MSVC++)*/
        cout << "ptr != alias" << endl;
        return (EXIT_FAILURE);
    }

    cout << "ptr == alias" << endl;
    return (EXIT_SUCCESS);

}
---------------<end>---------------


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