我有以下 C++ 代码:
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)中获得以下错误
|| 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'
我试过使用下面的代码:
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 );
}
};
但由于类型双关,启用优化时它会中断。
那么,为什么我的 used
属性被忽略了?
是否有一种解决方法可以在模板中转换字节序(我依靠枚举来避免类型双关)?
最佳答案
我只有 gcc 4.2.1,但如果我去掉 attribute((已使用))并为 union 命名,它会在没有警告的情况下编译。
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 ”技术适用于 gcc,但在标准中没有保证,其他“reinterpret_cast”方法是错误的(因为类型别名)。但是我认为这适用于 C,不确定 C++。希望对您有所帮助。
关于c++ - 字节顺序转换和 g++ 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2667225/