Mon. Mar 30th, 2026

enum class as Bit Flags

enum class Problem : uint8_t
{
    None        = 0,
    FileError   = 1 << 0,   // 1
    ParseError  = 1 << 1,   // 2
    Timeout     = 1 << 2,   // 4
    Network     = 1 << 3    // 8
};

Enable Bitwise Operators

inline Problem operator|(Problem a, Problem b)
{
    return static_cast<Problem>(
        static_cast<uint8_t>(a) |
        static_cast<uint8_t>(b)
    );
}

inline Problem operator&(Problem a, Problem b)
{
    return static_cast<Problem>(
        static_cast<uint8_t>(a) &
        static_cast<uint8_t>(b)
    );
}

inline Problem& operator|=(Problem& a, Problem b)
{
    a = a | b;
    return a;
}

Returning Combined Problems

Problem checkStuff()
{
    Problem result = Problem::None;

    if (!fileOk)
        result |= Problem::FileError;

    if (!parsed)
        result |= Problem::ParseError;

    return result;
}

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *