This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
No warning/error reported when non-constant reference is an rvalue
- To: bug-gcc at gnu dot org
- Subject: No warning/error reported when non-constant reference is an rvalue
- From: Nilesh Asher <nasher at ichips dot intel dot com>
- Date: Fri, 22 Sep 2000 16:07:26 -0700 (PDT)
GCC version type: 2.95.1
System type: HP-UX 10.20
Problem(s): g++ does not print a warning when an intialized for a non-constant
reference is used as an rvalue. Also, it gives different results if the syntax
is used without -O2 and with -O2.
Reference:
IAW ISO/IEC 14882:1998, 3.10[6], 5.4[1], the typecast 'short(y)' creates a
temporary rvalue object of type 'short'. The call to function t() then tries
to bind the temporary rvalue object to a non-const reference type (i.e.,
function t()'s short& argument 'x') -- which is not allowed (8.3[5]).
-----------------------------------------------------------------------------
casting_2.cpp:
-------------
#include <iostream.h>
void
t(short& x)
{
x = 0xF0;
}
int
main()
{
unsigned y = 0;
t(short(y));
cout << "Return value = " << hex << y << endl;
return 0;
}
-----------------------------------------------------------------------------
% g++ -g -Wall -pedantic-errors -ansi casting_2.cpp
% a.out
Return value = f0
% g++ -O2 -g -Wall -pedantic-errors -ansi casting_2.cpp
% a.out
Return value = 0