0

I have a enum that I am trying to create operator overloads. I am struggling with the boolean comparison operator.

Here is what I have:

enum class TraceLevel : uint32_t {

    // Basic logging levels (may be combined with trace level)
    All = 0xFFFFFFFF,
    None = 0x00000000,
    Info = 0x00000001,
    Warning = 0x00000002,
    Error = 0x00000004,
    Logging = 0x0000000F,
};

inline bool operator&&(TraceLevel __x, TraceLevel __y) {
    return static_cast<uint32_t>(__x & __y) > 0;
}

inline constexpr TraceLevel
operator&(TraceLevel __x, TraceLevel __y) {
    return static_cast<TraceLevel>
        (static_cast<uint32_t>(__x) & static_cast<uint32_t>(__y));
}

So with this enum class, I can issue the statements:

LogLevel a = LogLevel::Info;
LogLevel b = LogLevel::Warning;

LogLevel c = a & b;

But I also want to do:

if( a && b) {
   //do work
}

My inline operator declaration for && is not correct, but I am not sure what to change it to.

Ideas?

1
  • Don't prefix your identifiers with underscores. It's reserved for the implementation. Also, what are the semantics you expect for your operator &&? Currently, it will only return true if both operands have at least one common bit set. Only you can tell whether this is what you want. Commented Feb 18, 2015 at 23:01

1 Answer 1

1

There's a couple of problems with your code

  1. You define TraceLevel but use LogLevel
  2. Your definition of operator& is after your use of it in the definition of operator&&

I have renamed LogLevel to be TraceLevel and have moved the definition of operator& to be before the definition of operator&& (and as suggested in the comment removed underscores), see working example here.

#include <iostream>

enum class TraceLevel : uint32_t {

    // Basic logging levels (may be combined with trace level)
    All = 0xFFFFFFFF,
    None = 0x00000000,
    Info = 0x00000001,
    Warning = 0x00000002,
    Error = 0x00000004,
    Logging = 0x0000000F,
};

inline constexpr TraceLevel
operator&(TraceLevel x, TraceLevel y) {
    return static_cast<TraceLevel>
        (static_cast<uint32_t>(x) & static_cast<uint32_t>(y));
}

inline bool operator&&(TraceLevel x, TraceLevel y) {
    return static_cast<uint32_t>(x & y) > 0;
}

int main() {

    TraceLevel a = TraceLevel::Info;
    TraceLevel b = TraceLevel::Warning;

    TraceLevel c = a & b;

    if( a && b) {
        std::cerr << "a && b is true\n";
    } else {
        std::cerr << "a && b is false\n";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

On #1, yeah, that was a typo. I actually had it in my program as TraceLevel LogLevel = TraceLevel::Warning; Got them corn-fused when I retyped. As far as the order, ah, that makes sense. However, even changing the order VS2013 still chokes on it. I get these three errors when I try to put it in the way it works in Coliru: error C2146: syntax error : missing ';' before identifier 'TraceLevel', error C2433: 'constexpr' : 'inline' not permitted on data declarations and finally error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Doh!! Nevermind...my bad. Thanks for the tip on the order!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.