problem with enum and aliasing for gcc4.0.2?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a problem with enum and aliasing with a g++ gcc 4.0.2 compile that used to work on gcc3.2.3.

It circles around the following construct ++((int &)t) where t is an enum in a for loop.

I managed to strip it down to the following example:

class TOP {
public:
       class TEST;
       enum MY_ENUM {
       FIRST_MY_ENUM = 0,   // use to iterate in "for" loops
       MY_ENUM_MIN = 0,
       ENUM_0 = 0,
       ENUM_1 = 1,
       ENUM_2 = 2,
       ENUM_3 = 3,
       MY_ENUM_MAX = 4,
       INVALID_MY_ENUM = 4  // use to iterate in "for" loops
   };
#ifndef FIX
       inline static void incr(MY_ENUM &t) {++((int &)t);}
#else
       inline static void incr(MY_ENUM &t){
               int i=t;
       i++;
       t=(TOP::MY_ENUM)i;
#endif
};

class TOP::TEST {
public:
       TEST(){
               for ( int i = FIRST_MY_ENUM; i < INVALID_MY_ENUM; ++i) {
           _data[i]=0.0;
       }
       }
       TEST(const TEST &that){
for ( TOP::MY_ENUM i = TOP::FIRST_MY_ENUM; i < TOP::INVALID_MY_ENUM;
incr(i)) {
                       _data[i]=that._data[i];
               }
       }
       double get_data(){
               return _data[0];
       }
private:
      double _data[INVALID_MY_ENUM];
    };

TOP::TEST testclass;
TOP::TEST testclass2(testclass);

main(){
       //cout<<testclass.get_data()<<'\n';
       //cout<<testclass2.get_data()<<'\n';

}

If compiled on -O2 or -O3 it will hang forever in the second constructer. If -fno-strict-aliasing is added it passes. I actually beleave that if I would replace the constructors to function and call them I would still run into the same problem but this is the construct as I found it. I am not alble to get warning related to aliasing. I am not sure if this is actually valid code or a gcc bug. In case it is not a warning would have been nice. If compiled with -DFIX and thus using the other version of the incr function it works fine.

Thanks

Dirk-Jan

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux