Endianness conversion and g++ warnings

有些话、适合烂在心里 提交于 2019-12-06 13:37:20

I only have gcc 4.2.1 but if I get rid of the attribute ((used)) and give the union a name it compiles without warnings for me.

  inline static val fromLittleEndianToHost( val v )
  {
        union
        {
          val   outVal ;
          uint8_t bytes[ sizeof( val ) ] ;
        } u;

        u.outVal = v;
        std::reverse( &u.bytes[0], &u.bytes[ sizeof(val) ] );

        return u.outVal;
  }

From what I've read the 'union' technique works on gcc but is not guaranteed in the standard, the other 'reinterpret_cast' method is wrong (because of type aliasing). However I think this applies to C, not sure about C++. Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!