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: unassig...@gcc.gnu.org
        ReportedBy: tmosc...@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>---------------

Reply via email to