Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Created May 21, 2021 15:48
Show Gist options
  • Save Trass3r/0b79ca7ce7664871fc17a9d21f742c4b to your computer and use it in GitHub Desktop.
Save Trass3r/0b79ca7ce7664871fc17a9d21f742c4b to your computer and use it in GitHub Desktop.
#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