1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

Add enum fusing function (#127)

This commit is contained in:
Pavel I. Kryukov 2022-02-10 20:58:59 +03:00 committed by GitHub
parent 22242a613a
commit 1f8e29b140
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 0 deletions

View file

@ -12,6 +12,7 @@
* [`enum_index` obtains index in enum value sequence from enum value.](#enum_index)
* [`enum_contains` checks whether enum contains enumerator with such value.](#enum_contains)
* [`enum_type_name` returns type name of enum.](#enum_type_name)
* [`enum_fuse` returns a bijective mix of enum values.](#enum_fuse)
* [`is_unscoped_enum` checks whether type is an Unscoped enumeration.](#is_unscoped_enum)
* [`is_scoped_enum` checks whether type is an Scoped enumeration.](#is_scoped_enum)
* [`underlying_type` improved UB-free "SFINAE-friendly" underlying_type.](#underlying_type)
@ -325,6 +326,25 @@ constexpr string_view enum_type_name() noexcept;
// color_name -> "Color"
```
## `enum_fuse`
```cpp
template<typename ... Es>
[[nodiscard]] constexpr size_t enum_fuse(Es ... values);
```
* Returns a bijective mix of several enum values with [Cantor pairing function](https://en.wikipedia.org/wiki/Pairing_function). This can be used to emulate 2D switch/case statements.
* Examples
```cpp
switch (magic_enum::enum_fuse(color1, color2)) {
case magic_enum::enum_fuse(RED, BLUE): // ...
case magic_enum::enum_fuse(RED, RED): // ...
// ...
}
```
## `is_unscoped_enum`
```cpp