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

2010-12-20 Thread tmoschou at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47026

Terry  changed:

   What|Removed |Added

 CC||tmoschou at gmail dot com

--- Comment #1 from Terry  2010-12-20 11:33:37 UTC 
---
Whoops, forgot to say compiled on Ubuntu 10.10 x64.


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

2010-12-20 Thread tmoschou at gmail dot com
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
-
#include 
#include 

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);

}
--