有人可以推荐一种更优雅的方式来实现这些编译时常量吗?
template <int> struct Map;
template <> struct Map<0> {static const int value = 4;};
template <> struct Map<1> {static const int value = 8;};
template <> struct Map<2> {static const int value = 15;};
template <int> struct MapInverse;
template <> struct MapInverse<4> {static const int value = 0;};
template <> struct MapInverse<8> {static const int value = 1;};
template <> struct MapInverse<15> {static const int value = 2;};
Run Code Online (Sandbox Code Playgroud)
这个值需要在我的程序中是constexpr,但逆映射值更新很繁琐(容易出错或忘记做).