字节顺序转换和g ++警告 [英] Endianness conversion and g++ warnings

查看:124
本文介绍了字节顺序转换和g ++警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C ++代码:

I've got the following C++ code :

template <int isBigEndian, typename val>
struct EndiannessConv
{
    inline static val fromLittleEndianToHost( val v )
    {
        union
        {
            val   outVal __attribute__ ((used));
            uint8_t bytes[ sizeof( val ) ] __attribute__ ((used));
        } ;

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

        return outVal;
    }

    inline static void convertArray( val v[], uint32_t size )
    {
        // TODO : find a way to map the array for (uint32_t i = 0; i < size; i++)
        for (uint32_t i = 0; i < size; i++)
            v[i] = fromLittleEndianToHost( v[i] );
    }
};

哪些工作和测试(没有使用的属性)。编译时,我从g ++(版本4.4.1)获取以下错误

Which work and has been tested (without the used attributes). When compiling I obtain the following errors from g++ (version 4.4.1)

|| g++ -Wall -Wextra -O3 -o t t.cc
|| t.cc: In static member function 'static val EndiannessConv<isBigEndian, val>::fromLittleEndianToHost(val)':
t.cc|98| warning: 'used' attribute ignored
t.cc|99| warning: 'used' attribute ignored
|| t.cc: In static member function 'static val EndiannessConv<isBigEndian, val>::fromLittleEndianToHost(val) [with int isBigEndian = 1, val = double]':
t.cc|148| instantiated from here
t.cc|100| warning: unused variable 'outVal'
t.cc|100| warning: unused variable 'bytes'

我尝试使用下面的代码:

I've tried to use the following code :

template <int size, typename valType>
struct EndianInverser { /* should not compile */ };

template <typename valType>
struct EndianInverser<4, valType>
{ 
    static inline valType reverseEndianness( const valType &val )
    {
        uint32_t    castedVal =
            *reinterpret_cast<const uint32_t*>( &val );
        castedVal = (castedVal & 0x000000FF << (3 * 8))
                | (castedVal & 0x0000FF00 << (1 * 8))
                | (castedVal & 0x00FF0000 >> (1 * 8))
                | (castedVal & 0xFF000000 >> (3 * 8));

        return *reinterpret_cast<valType*>( &castedVal );
    }
};

但由于类型冲突而启用优化时会中断。

but it break when enabling optimizations due to the type punning.

那么,为什么我的使用属性被忽略?
在模板中是否有转换字节序的解决方法(我依靠枚举来避免类型转换)

So, why does my used attribute got ignored? Is there a workaround to convert endianness (I rely on the enum to avoid type punning) in templates?

推荐答案

我只有gcc 4.2.1,但如果我摆脱属性((使用)),并给联合的名称它编译,没有警告为我。

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;
  }



从我读过的'union'不保证在标准中,另一个'reinterpret_cast'方法是错误的(因为类型别名)。但我认为这适用于C,不知道C ++。希望有所帮助。

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.

这篇关于字节顺序转换和g ++警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆