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

Nilesh Asher
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.

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]).

#include <iostream.h>

t(short& x)
    x = 0xF0;

    unsigned y = 0;

    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