Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loss of const volatile qualifiers

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.

like image 505
Twifty Avatar asked Jul 19 '13 14:07

Twifty


People also ask

What is the meaning of volatile qualifier in C?

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. ...

Which expression would lose some const-volatile qualifiers to call'function'?

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.

How to change the value of a volatile variable?

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.

What are the different types of type qualifiers in C++?

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.


1 Answers

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.

like image 200
James Kanze Avatar answered Oct 14 '22 05:10

James Kanze