Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
This is second part of my C++ Telltales series where I share some tips and tricks to work with C++. Feel free to check out also other parts of the series from my profile!
C++ enums are useful when you have to create a type that can be used as a flag. To use them as flags, they need to have operators to compare if specific flag is set or to provide a combination of enumeration values. To prevent extra work (or if you are just feeling plain lazy), you can introduce shared operators for all enums by utilizing templates. Here is a small snippet how to do that:
What’s great about this solution is that it only works for enumerations due to std::enable_if type trait and only for enumerations in the same namespace. To utilize it to the maximum, just keep your flag enumerations in same namespace and remember to include the header file you have the templates declared.
And here is the same image from my previous part to get some cover photo for this article (I am feeling lazy..):
If you liked the story, please press the ❤ button below (did you know that you can give more than one clap). Also please feel free to share this story!
About me
I am Heikki Hellgren, Software Expert and technology enthusiast working at Elektrobit Automotive. My interests are in software construction, tools, automatic testing and all the new and cool stuff like AI and autonomous driving. You can follow me on Medium and Twitter and check out my websitefor more information.
Shared Enum Operators was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.