No warning/error reported when non-constant reference is an rvalue

Nilesh Asher nasher@ichips.intel.com
Sat Sep 23 15:50:00 GMT 2000


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


More information about the Gcc-bugs mailing list