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.
Qualifier in C (Const and Volatile) 1 Qualifier in C 2 Introduction. ... 3 Const or Constant (qualifier in c) The const keyword in a declaration establishes a variable whose value cannot be modified by assignment or by incrementing or decrementing. 4 Volatile (qualifier in c) VOLATILE means: – UNSTABLE, UNPREDICTABLE…etc. ... 5 Conclusion. ...
In this article expression having type 'type' would lose some const-volatile qualifiers in order to call 'function' A variable with a specified const-volatile type can only call member functions defined with same or greater const-volatile qualifications.
A volatile variable can be changed by the background routine of the pre-processor. This background routine may be interrupt signals by a microprocessor, threads, real times clocks, etc. In simple word, we can say a value volatile variable which has stored in the memory can be by any external sources.
In that list, const and volatile are type qualifiers, the rest are type specifiers. Various combinations of type specifiers are permitted: char signed char, unsigned char int, signed int, unsigned int short int, signed short int, unsigned short int long int, signed long int, unsigned long int float A few points should be noted.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With