Created
May 21, 2021 15:48
-
-
Save Trass3r/0b79ca7ce7664871fc17a9d21f742c4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <https://raw.githubusercontent.com/swansontec/map-macro/master/map.h> | |
#define CONST_MAP_CASE2(a, b) case a: return b; | |
#define CONST_MAP_CASE(pair) CONST_MAP_CASE2 pair | |
#define HEAD(h, ...) h | |
#define CONST_MAP_RET2(a, b) return decltype(b){}; | |
#define CONST_MAP_RET(pair) CONST_MAP_RET2 pair | |
#define CONST_MAP(name, ...) \ | |
constexpr auto name = [](auto v){ \ | |
switch (v) { \ | |
MAP(CONST_MAP_CASE, __VA_ARGS__) \ | |
} \ | |
CONST_MAP_RET(HEAD(__VA_ARGS__)) \ | |
} | |
enum E | |
{ | |
A, | |
B | |
}; | |
CONST_MAP(mapEnum, (A, 1), (B, 2)); | |
auto foo(E e) | |
{ | |
return mapEnum(e); | |
} | |
auto v = mapEnum(A); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment