I have a compile error:
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 &)'
The description of this error, Here, doesn't really explain what's going on.
I'm unable to reproduce the error in a smaller sample, but I can show the basic layout of my class.
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
}
};
The error popped up after adding endian_swap
as a member. The actual class is derived from std::codecvt
and installed into an std::locale
.
Can somebody give a better explanation of the error than the site above.
Edit: Actual code:
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) };
};
Update: Found it! Look carefully at the above template and see if you can see it too.
How are you instantiating the template. Your instantiation of
endian_swap
uses type T
, and you pass it an unsigned
short
. Unless T
is unsigned short
, you'll need
a conversion, and the results of a conversion are a temporary,
which can't bind to a non-const reference.
You don't provide a SSCCE, so it's hard to say. But the error
message you post refers to a call to unsigned long unicode::endian_swap<T>::operator ()(T &);
either
endian_swap
is instantiated for unsigned long
(in which
case, passing an unsigned short
would require a temporary), or
the code you posted is not the code which triggered the error.