Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <utility>
  2.  
  3. template <typename T, typename U>
  4. T RandomChoice_Recursive(unsigned choice, unsigned i, U a)
  5. {
  6. return a;
  7. }
  8.  
  9. template <typename T, typename U, typename... TArgs>
  10. T RandomChoice_Recursive(unsigned choice, unsigned i, U a, TArgs... tail)
  11. {
  12. if (i == choice)
  13. return a;
  14. else
  15. return RandomChoice_Recursive<T>(choice, i+1, std::forward<TArgs>(tail)...);
  16. }
  17.  
  18. template <typename T, typename... TArgs>
  19. T RandomChoice(T a, TArgs... tail)
  20. {
  21. unsigned num = sizeof...(TArgs) + 1;
  22. unsigned choice = rand();
  23. choice %= num;
  24. return RandomChoice_Recursive<T>(choice, 0, std::forward<T>(a), std::forward<TArgs>(tail)...);
  25. }
  26.  
  27. template <typename T, typename U, typename... UArgs>
  28. T RandomChoiceNew_Recursive(unsigned choice, unsigned i, UArgs... args)
  29. {
  30. return new U(args...);
  31. }
  32.  
  33. template <typename T, typename U, typename V, typename... TArgs, typename... UArgs>
  34. T RandomChoiceNew_Recursive(unsigned choice, unsigned i, UArgs... args)
  35. {
  36. if (i == choice)
  37. return new U(args...);
  38. else
  39. return RandomChoiceNew_Recursive<T, V, TArgs...>(choice, i+1, std::forward<UArgs>(args)...);
  40. }
  41.  
  42. template <typename T, typename... TArgs, typename... UArgs>
  43. T RandomChoiceNew(UArgs... args)
  44. {
  45. unsigned num = sizeof...(TArgs) + 1;
  46. unsigned choice = rand();
  47. choice %= num;
  48. return RandomChoiceNew_Recursive<T, TArgs...>(choice, 0, std::forward<UArgs>(args)...);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement