This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/15330] New: inconsistent slicing on cast of int to enum


When compiled with gcc 3.4 (but not 3.3) the program below produces inconsistent
results in the last three iterations of the loop, depending on the expression in
the cast to E. I am aware of 7.2, p9 but I'm having trouble coming up with a a
situation where this inconsistency could be useful. It seems that consistent
behavior should be preferred. Specifically, if the enumertaion is big enough to
fit the integer as it is in this case the integer value should be represented
without change.

Martin

$ cat t.cpp && g++ t.cpp -static && ./a.out
#include <stdio.h>

enum E { e = 0x10000 };

int main ()
{
    int a [] = {
        E (0x1), E (0x10), E (0x100), E (0x1000), E (0x10000),
        E (0x100000), E (0x1000000), E (0x10000000)
    };

    for (int i = 0; i != sizeof a / sizeof *a; ++i) {
        int j = E (1 << (i * 4));
        printf ("1 << %d: %#x == %#x\n", i * 4, a [i], j);
    }
}

1 << 0: 0x1 == 0x1
1 << 4: 0x10 == 0x10
1 << 8: 0x100 == 0x100
1 << 12: 0x1000 == 0x1000
1 << 16: 0x10000 == 0x10000
1 << 20: 0 == 0x100000
1 << 24: 0 == 0x1000000
1 << 28: 0 == 0x10000000

-- 
           Summary: inconsistent slicing on cast of int to enum
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sebor at roguewave dot com
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: sparc-solaris


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15330


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]