0

I want to use a 24bit signed integer data type lockfree with atomic. I tried, but the compiler gives me this error:

boost::atomic requires T to be a trivially copyable type

Visibly, the data type I'm using is not trivially copyable.

const int INT24_MAX = 8388607;

class Int24
{
 protected:
    unsigned char m_Internal[3];
 public:
    Int24()
    {
    }

    Int24(const int val)
    {
        *this = val;
    }

    Int24(const Int24& val)
    {
        *this = val;
    }

    operator int() const
    {
       if (m_Internal[2] & 0x80) // Is this a negative?  Then we need to siingn extend.
       {
        return (0xff << 24) | (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
       }
        else
        {
            return (m_Internal[2] << 16) | (m_Internal[1] << 8) | (m_Internal[0] << 0);
        }
    }

    operator float() const
    {
        return (float)this->operator int();
    }

    Int24& operator =(const Int24& input)
    {
        m_Internal[0] = input.m_Internal[0];
        m_Internal[1] = input.m_Internal[1];
        m_Internal[2] = input.m_Internal[2];

        return *this;
    }

    Int24& operator =(const int input)
    {
        m_Internal[0] = ((unsigned char*)&input)[0];
        m_Internal[1] = ((unsigned char*)&input)[1];
        m_Internal[2] = ((unsigned char*)&input)[2];

        return *this;
    }

    Int24 operator +(const Int24& val) const
    {
        return Int24((int)*this + (int)val);
    }

    Int24 operator -(const Int24& val) const
    {
        return Int24((int)*this - (int)val);
    }

    Int24 operator *(const Int24& val) const
    {
        return Int24((int)*this * (int)val);
    }

    Int24 operator /(const Int24& val) const
    {
        return Int24((int)*this / (int)val);
    }

    /***********************************************/

    Int24 operator +(const int val) const
    {
        return Int24((int)*this + val);
    }

    Int24 operator -(const int val) const
    {
        return Int24((int)*this - val);
    }

    Int24 operator *(const int val) const
    {
        return Int24((int)*this * val);
    }

    Int24 operator /(const int val) const
    {
        return Int24((int)*this / val);
    }

    /***********************************************/
   


    Int24& operator +=(const Int24& val)
    {
        *this = *this + val;
        return *this;
    }

    Int24& operator -=(const Int24& val)
    {
        *this = *this - val;
        return *this;
    }

    Int24& operator *=(const Int24& val)
    {
        *this = *this * val;
        return *this;
    }

    Int24& operator /=(const Int24& val)
    {
        *this = *this / val;
        return *this;
    }

    /***********************************************/

    Int24& operator +=(const int val)
    {
        *this = *this + val;
        return *this;
    }

    Int24& operator -=(const int val)
    {
        *this = *this - val;
        return *this;
    }

    Int24& operator *=(const int val)
    {
        *this = *this * val;
        return *this;
    }

    Int24& operator /=(const int val)
    {
        *this = *this / val;
        return *this;
    }

    /***********************************************/
 

    Int24 operator >>(const int val) const
    {
        return Int24((int)*this >> val);
    }

    Int24 operator <<(const int val) const
    {
        return Int24((int)*this << val);
    }

    /***********************************************/

    Int24& operator >>=(const int val)
    {
        *this = *this >> val;
        return *this;
    }

   
    Int24& operator <<=(const int val)
    {
        *this = *this << val;
        return *this;
    }


    /***********************************************/
   

    operator bool() const
    {
        return (int)*this != 0;
    }

    bool operator !() const
    {
        return !((int)*this);
    }

    Int24 operator -()
    {
        return Int24(-(int)*this);
    }

    /***********************************************/
   

    bool operator ==(const Int24& val) const
    {
        return (int)*this == (int)val;
    }

    bool operator !=(const Int24& val) const
    {
        return (int)*this != (int)val;
    }

    bool operator >=(const Int24& val) const
    {
        return (int)*this >= (int)val;
    }

    bool operator <=(const Int24& val) const
    {
        return (int)*this <= (int)val;
    }

    bool operator >(const Int24& val) const
    {
        return (int)*this > (int)val;
    }

    bool operator <(const Int24& val) const
    {
        return (int)*this < (int)val;
    }

    /***********************************************/

    bool operator ==(const int val) const
    {
        return (int)*this == val;
    }

    bool operator !=(const int val) const
    {
        return (int)*this != val;
    }

    bool operator >=(const int val) const
    {
        return (int)*this >= val;
    }

    bool operator <=(const int val) const
    {
        return (int)*this <= val;
    }

    bool operator >(const int val) const
    {
        return ((int)*this) > val;
    }

    bool operator <(const int val) const
    {
        return (int)*this < val;
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Do you know what it means for a class to be [trivally copyable](https://en.cppreference.com/w/cpp/named_req/TriviallyCopyable)? All of the constructors and assignment operators you've defined only _remove_ functionality from your class without gaining you anything. You probably just want to remove them. See also [C.20](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-zero) from the C++ core guidelines. – Brian61354270 Nov 20 '21 at 00:47
  • To clarify, my comment above only applies to the default constructors, copy constructors, and copy assignment operator. The ones that accept an `int` are a-okay. – Brian61354270 Nov 20 '21 at 00:56
  • 1
    Atomic operations only work on native types supported by the CPU architecture, and most CPUs don't support a native 24bit integer type. – Remy Lebeau Nov 20 '21 at 05:50

0 Answers0