1

I made bit flags using a scoped enum, and I overload operator | to combine values:

enum class PARAM_T : int {
    NONE = 0x0,
    INPUT = 0x01,
    OUTPUT = 0x02,
    OUTPUT_VECTOR = 0x04
};

inline PARAM_T operator | (PARAM_T lhs, PARAM_T rhs)
{
    using T = std::underlying_type_t<PARAM_T>;
    return (PARAM_T)(static_cast<T>(lhs) | static_cast<T>(rhs));
}

Elsewhere in my project, I do some drag/drop operations using Qt widgets, where I use operator | to combine some named constants:

Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);

The operator | here is unrelated to my bit flags, yet for some reason my overload breaks this line of code, giving the following error:

error C2664: 'Qt::DropAction QDrag::exec(Qt::DropActions,Qt::DropAction)' : cannot convert argument 1 from 'int' to 'Qt::DropActions'

Why is the compiler matching Qt constants to my overload? They are entirely different and incompatible types.

Carlton
  • 4,217
  • 2
  • 24
  • 40
  • This doesn't look possible. Can you provide an MCVE? – n. m. could be an AI Jul 30 '15 at 16:26
  • I can't reproduce the problem with my MCVE. I'm trying to figure out what code I have to add to make it fail as it fails in my project. What about this doesn't look possible, BTW? – Carlton Jul 30 '15 at 18:24
  • You should use `QFlags` and then you'll get type safety and operators, even on C++98. – Kuba hasn't forgotten Monica Jul 30 '15 at 18:45
  • It works fine with Qt 5.5/mingw 4.9.2/std=c++14 flag. "What about this doesn't look possible?": your issue, precisely because "They are entirely different types" (you can use `static_cast<>` though). It is suggested to use `QFlags` as a workaround, not to abandon the enum. – Armaghast Aug 05 '15 at 15:10
  • Possible duplicate of [QFlags Enum Type Conversion fails all of a sudden](http://stackoverflow.com/questions/10755058/qflags-enum-type-conversion-fails-all-of-a-sudden) – isanae Feb 06 '16 at 01:28

1 Answers1

0

Reading up on the subject of flags, I see that there are arguments against using enums as flags, so I changed my enum to this:

namespace PARAM {
    const int NONE =            0;
    const int INPUT =           1 << 0;
    const int OUTPUT_SCALAR =   1 << 1;
    const int OUTPUT_VECTOR =   1 << 2;
}

Now I can use the default implementations of operator |, operator &, etc, instead of my overloads, and Qt is happy again. This is more of a work-around than an answer; I'd still like to know why the compiler was using my overload for the Qt constants.

Carlton
  • 4,217
  • 2
  • 24
  • 40