zhangsongcui

Unsequenced function call

Apr 20th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <cstdio>
  2. #include <tuple>
  3.  
  4. template <typename... _tFake>
  5. constexpr void unsequenced(_tFake... fake)
  6. {
  7. }
  8.  
  9. template<size_t... _vSeq>
  10. struct unseq_dispatcher
  11. {
  12.     template<typename _fCallable, typename... _tParams>
  13.     static inline void
  14.     call(_fCallable f, _tParams&&... args)
  15.     {
  16.         unsequenced((f(std::get<_vSeq>(std::forward_as_tuple(args...))), 0)...);
  17.     }
  18. };
  19.  
  20. template <size_t m, size_t idx = 0>
  21. struct make_natrual_sequence
  22. {
  23.     template <size_t... ipara>
  24.     using type = typename make_natrual_sequence<m, idx+1>::template type<ipara..., idx>;
  25. };
  26.  
  27. template<size_t m>
  28. struct make_natrual_sequence<m, m>
  29. {
  30.     template <size_t... ipara>
  31.     using type = unseq_dispatcher<ipara...>;
  32. };
  33.  
  34. template<typename _fCallable, typename... _tParams>
  35. inline void
  36. unseq_apply(_fCallable f, _tParams&&... args)
  37. {
  38.     make_natrual_sequence<sizeof...(_tParams)>::template type<>::call(f, std::forward<_tParams>(args)...);
  39. }
  40.  
  41. int main()
  42. {
  43.     using namespace std;
  44.     auto f = [](int a){printf("%d", a);};
  45.     unseq_apply(f, 1, 2, 3, 4, 5);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment