我有一个编译错误:
error C3848: expression having type 'const unicode::endian_swap<T>' would lose some const-volatile qualifiers in order to call 'unsigned long unicode::endian_swap<T>::operator ()(T &)'
此错误的描述Here并没有真正解释发生了什么。
我无法在较小的样本中重现错误,但我可以展示我班级的基本布局。
template < typename T >
struct endian_swap
{
endian_swap ( void ) {}
T operator () ( T& _val ) const { return _val >> 8 | _val << 8; }
};
template < typename T >
struct test
{
endian_swap< T > _swap;
virtual void do_it ( ) const
{
unsigned short n = 0x1234;
unsigned short * _dest = &n;
*_dest++ = _swap( n ); // <-- Error is here
}
};
endian_swap
添加为成员后弹出错误。实际的类派生自std::codecvt
并安装到std::locale
.
有人可以比上面的网站更好地解释错误。
编辑:实际代码:
template < typename T, size_t N = sizeof( T ) > struct endian_swap
{
endian_swap ( void ) {};
T operator () ( const T _val ) const { return _val };
};
template < typename T > struct endian_swap< T, 2 >
{
endian_swap ( void ) {}
T operator () ( const T _val ) const { return _val >> 8 | _val << 8; }
};
template < typename T > struct endian_swap< T, 4 >
{
endian_swap ( void ) {};
T operator () ( const T _val ) const { return (_val >> 24) | ((_val & 0x00ff0000) >> 8) | ((_val & 0x0000ff00) << 8) | (_val << 24) };
};
更新:找到了!仔细看上面的模板,看看你是否也能看到它。