This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/47026] New: invalid temporary is being assigned to a const-reference
- From: "tmoschou at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 20 Dec 2010 11:30:50 +0000
- Subject: [Bug c++/47026] New: invalid temporary is being assigned to a const-reference
- Auto-submitted: auto-generated
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>---------------