Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overload |= operator on scoped enum?

How can I overload the |= operator on a strongly typed (scoped) enum (in C++11, GCC)?

I want to test, set and clear bits on strongly typed enums. Why strongly typed? Because my books say it is good practice. But this means I have to static_cast<int> everywhere. To prevent this, I overload the | and & operators, but I can't figure out how to overload the |= operator on an enum. For a class you'd simply put the operator definition in the class, but for enums that doesn't seem to work syntactically.

This is what I have so far:

enum class NumericType {     None                    = 0,      PadWithZero             = 0x01,     NegativeSign            = 0x02,     PositiveSign            = 0x04,     SpacePrefix             = 0x08 };  inline NumericType operator |(NumericType a, NumericType b) {     return static_cast<NumericType>(static_cast<int>(a) | static_cast<int>(b)); }  inline NumericType operator &(NumericType a, NumericType b) {     return static_cast<NumericType>(static_cast<int>(a) & static_cast<int>(b)); } 

The reason I do this: this is the way it works in strongly-typed C#: an enum there is just a struct with a field of its underlying type, and a bunch of constants defined on it. But it can have any integer value that fits in the enum's hidden field.

And it seems that C++ enums work in the exact same way. In both languages casts are required to go from enum to int or vice versa. However, in C# the bitwise operators are overloaded by default, and in C++ they aren't.

like image 385
Daniel A.A. Pelsmaeker Avatar asked Apr 08 '13 21:04

Daniel A.A. Pelsmaeker


People also ask

How do you overload an operator in Python?

In Python, overloading is achieved by overriding the method which is specifically for that operator, in the user-defined class. For example, __add__(self, x) is a method reserved for overloading + operator, and __eq__(self, x) is for overloading == .

What is a scoped enum?

A scoped enum looks exactly as a traditional enum except that the keyword class (or struct – the two keywords are interchangeable in this context) appears between the keyword enum and the enum name, as shown in the following example: enum class Color //C++11 scoped enum.

Which operator is overloaded by OR () function in Python?

Which operator is overloaded by the __or__() function? Explanation: The function __or__() overloads the bitwise OR operator |.

Which of the following methods is used by a user defined class to support '+' operator?

Defining methods for operators is known as operator overloading. For e.g: To use + operator with custom objects you need to define a method called __add__ . In the above example we have added __add__() method which allows use to use the + operator to add two circle objects.


1 Answers

inline NumericType& operator |=(NumericType& a, NumericType b) {     return a= a |b; } 

This works? Compile and run: (Ideone)

#include <iostream> using namespace std;  enum class NumericType {     None                    = 0,      PadWithZero             = 0x01,     NegativeSign            = 0x02,     PositiveSign            = 0x04,     SpacePrefix             = 0x08 };  inline NumericType operator |(NumericType a, NumericType b) {     return static_cast<NumericType>(static_cast<int>(a) | static_cast<int>(b)); }  inline NumericType operator &(NumericType a, NumericType b) {     return static_cast<NumericType>(static_cast<int>(a) & static_cast<int>(b)); }  inline NumericType& operator |=(NumericType& a, NumericType b) {     return a= a |b; }  int main() {     // your code goes here     NumericType a=NumericType::PadWithZero;     a|=NumericType::NegativeSign;     cout << static_cast<int>(a) ;     return 0; } 

print 3.

like image 182
qPCR4vir Avatar answered Sep 22 '22 19:09

qPCR4vir