[英]C++ Overloading | (bitwise OR) operator that returns constant value
I would like to use operator '|' 我想使用运算符'|' in a following manner:
以以下方式:
switch(color){
case Color::Red | Color::Green:
...
The problem is that the operator must return constant value, but I cannot make it work. 问题是运算符必须返回常量值,但我无法使其正常工作。 I tried something like this:
我尝试过这样的事情:
template<class T> inline const T operator| (T a, T b){ return const (T)((int)a | (int)b); }
but it doesn't do the job. 但这没有做。
case
should use constant expression, ie compile-time constant. case
应使用常量表达式,即编译时常量。 Mark your operator as constexpr
: 将您的运算符标记为
constexpr
:
template<class T> inline constexpr T operator| (T a, T b){ return (T)((int)a | (int)b); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.