[Bug c++/64852] New: C++ accepts invalid(?) constants only at -O0

bernd.edlinger at hotmail dot de gcc-bugzilla@gcc.gnu.org
Thu Jan 29 02:35:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64852

            Bug ID: 64852
           Summary: C++ accepts invalid(?) constants only at -O0
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bernd.edlinger at hotmail dot de

This program should probably be rejected, but actually it compiles correctly
and
without warning, the resulting code "works".
Same behaviour with current trunk and 4.6.2



cat test.C
#include <arpa/inet.h>
#include <stdio.h>

const short test = htons(10);

int
main()
{
   printf("%x\n", test); 
}


g++ -O0 test.C && ./a.out 
a00

g++ -O1 test.C
In file included from /usr/include/x86_64-linux-gnu/bits/byteswap.h:35:0,
                 from /usr/include/endian.h:60,
                 from /usr/include/x86_64-linux-gnu/sys/types.h:216,
                 from /usr/include/x86_64-linux-gnu/sys/uio.h:23,
                 from /usr/include/x86_64-linux-gnu/sys/socket.h:26,
                 from /usr/include/netinet/in.h:23,
                 from /usr/include/arpa/inet.h:22,
                 from test.C:1:
test.C:4:20: error: statement-expressions are not allowed outside functions nor
in template-argument lists
 const short test = htons(10);



the same program is correctly rejected if compiled in C mode,
but interestingly with a completely different message on -O0 and -O1



More information about the Gcc-bugs mailing list