C++ C++;重载返回常量值的|(按位或)运算符

C++ C++;重载返回常量值的|(按位或)运算符,c++,C++,我希望以以下方式使用运算符“|”: switch(color){ case Color::Red | Color::Green: ... 问题是运算符必须返回常量值,但我无法使其工作。我试过这样的方法: template<class T> inline const T operator| (T a, T b){ return const (T)((int)a | (int)b); } 模板内联常量T运算符|(ta,tb){返回常量(

我希望以以下方式使用运算符“|”:

    switch(color){
        case Color::Red | Color::Green:
        ...
问题是运算符必须返回常量值,但我无法使其工作。我试过这样的方法:

    template<class T> inline const T operator| (T a, T b){ return const (T)((int)a | (int)b); }
模板内联常量T运算符|(ta,tb){返回常量(T)((int)a |(int)b)}

但是它不起作用。

case
应该使用常量表达式,即编译时常量。将操作员标记为
constexpr

template<class T> inline constexpr T operator| (T a, T b){ return (T)((int)a | (int)b); }
模板内联constexpr T运算符|(ta,tb){return(T)((int)a |(int)b)}

案例
应使用常量表达式,即编译时常量。将操作员标记为
constexpr

template<class T> inline constexpr T operator| (T a, T b){ return (T)((int)a | (int)b); }
模板内联constexpr T运算符|(ta,tb){return(T)((int)a |(int)b)}

try
template inline constexpr const T operator |(const T a,const T b){return const(T)((int)a |(int)b)}
而且你的主题有点误导性。您正在尝试使用逻辑OR或按位OR吗?
|
操作符是前者,
|
是后者。尝试
模板内联constexpr const T操作符|(const T a,const T b){返回const(T)((int)a |(int)b)}
你的主题也有点误导性。您正在尝试使用逻辑OR或按位OR吗?
|
运算符是前者,
|
运算符是后者。