Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct not_found
- {
- };
- template <typename TKey, typename TValue, TKey...>
- struct JumpTable;
- template <typename TKey, typename TValue>
- struct JumpTable<TKey, TValue>
- {
- TValue& operator[]([[maybe_unused]] TKey needed)
- {
- throw not_found{};
- }
- };
- template <typename TKey, typename TValue, TKey key, TKey... rest>
- struct JumpTable<TKey, TValue, key, rest...> : JumpTable<TKey, TValue, rest...>
- {
- TValue value;
- using super = JumpTable<TKey, TValue, rest...>;
- TValue& operator[](TKey k)
- {
- return key == k ? value : super::operator[](k);
- }
- };
- int fn1(int x)
- {
- return x + 1;
- }
- int fn2(int x)
- {
- return x + 2;
- }
- int fn3(int x)
- {
- return x + 3;
- }
- int main()
- {
- JumpTable<DispatchId,
- FuncType,
- DispatchId::Id1,
- DispatchId::Id2,
- DispatchId::Id3> jt;
- jt[DispatchId::Id1] = fn1;
- jt[DispatchId::Id2] = fn2;
- jt[DispatchId::Id3] = fn3;
- }
Add Comment
Please, Sign In to add comment