https://github.com/joeloser/enum-flag-set
Header-only library providing bit flags for enum classes.
https://github.com/joeloser/enum-flag-set
bitflags bitmask
Last synced: 11 months ago
JSON representation
Header-only library providing bit flags for enum classes.
- Host: GitHub
- URL: https://github.com/joeloser/enum-flag-set
- Owner: JoeLoser
- License: mit
- Created: 2018-01-20T23:21:00.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-02T22:02:10.000Z (almost 8 years ago)
- Last Synced: 2025-01-30T23:17:49.405Z (about 1 year ago)
- Topics: bitflags, bitmask
- Language: C++
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: contributing/commit_template
- License: LICENSE
Awesome Lists containing this project
README
# Bit Flags for enum classes
## Synopsis
This header-only library introduces a `flag_set` class template which provides bit
flags for enum classes.
## Motivation
While enum classes are great in that they are strongly typed and do not implicitly
cast to the underlying type, we cannot use the bitwise operators directly out
of the box because of this. So, we would like to define bitwise operators on some
type and have our type safety still.
The `flag_set` class template introduces this exact strong type and defines
bitwise operators on the underlying values the container holds.
## Code Example
[Try it out on Wandbox](https://wandbox.org/permlink/KzED2WMlJBYPQybg)
```c++
#include
#include "flag_set.hpp"
enum class ExampleEnumClass
{
One = 1,
Two = 1 << 1
};
USE_FLAGS_FOR_ENUM(ExampleEnumClass);
int main()
{
constexpr auto mask = ExampleEnumClass::One | ExampleEnumClass::Two;
if constexpr(mask)
{
// Do something :)
}
return EXIT_SUCCESS;
}
```