[英]Overloading operator | interferes with Qt enum constants
I made bit flags using a scoped enum, and I overload operator |
我使用作用域枚举创建位标志,并且重载了
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 |
在项目的其他地方,我使用Qt小部件执行一些拖放操作,在这里我使用
operator |
to combine some named constants: 合并一些命名常量:
Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
The operator |
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? 为什么编译器将Qt常量与我的重载匹配? They are entirely different and incompatible types.
它们是完全不同且不兼容的类型。
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 |
的默认实现。 , operator &
, etc, instead of my overloads, and Qt is happy again. ,
operator &
等,而不是我的重载,Qt再次感到高兴。 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.
我仍然想知道为什么编译器将我的重载用于Qt常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.