Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. struct Obj
  6. {
  7.   size_t v;
  8. };
  9.  
  10. template <size_t I>
  11. static Obj foo() { return Obj{I}; }
  12.  
  13. template <class T, size_t... I>
  14. static T switch_(size_t i, std::index_sequence<I...>)
  15. {
  16.   T ret;
  17.   const bool ar[] = {
  18.     ((i == I) ? (ret = foo<I>(),false):false, false)... };
  19.   return ret;
  20. }
  21.  
  22. template <class T, size_t Max>
  23. static T do_switch(size_t i)
  24. {
  25.   return switch_<T>(i, std::make_index_sequence<Max>());
  26. }
  27.  
  28. int main(int argc, char** argv)
  29. {
  30.   auto ret = do_switch<Obj, 10>(argc);
  31.   std::cout << ret.v << std::endl;
  32.   return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement