Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utility>
- template <typename T, typename U>
- T RandomChoice_Recursive(unsigned choice, unsigned i, U a)
- {
- return a;
- }
- template <typename T, typename U, typename... TArgs>
- T RandomChoice_Recursive(unsigned choice, unsigned i, U a, TArgs... tail)
- {
- if (i == choice)
- return a;
- else
- return RandomChoice_Recursive<T>(choice, i+1, std::forward<TArgs>(tail)...);
- }
- template <typename T, typename... TArgs>
- T RandomChoice(T a, TArgs... tail)
- {
- unsigned num = sizeof...(TArgs) + 1;
- unsigned choice = rand();
- choice %= num;
- return RandomChoice_Recursive<T>(choice, 0, std::forward<T>(a), std::forward<TArgs>(tail)...);
- }
- template <typename T, typename U, typename... UArgs>
- T RandomChoiceNew_Recursive(unsigned choice, unsigned i, UArgs... args)
- {
- return new U(args...);
- }
- template <typename T, typename U, typename V, typename... TArgs, typename... UArgs>
- T RandomChoiceNew_Recursive(unsigned choice, unsigned i, UArgs... args)
- {
- if (i == choice)
- return new U(args...);
- else
- return RandomChoiceNew_Recursive<T, V, TArgs...>(choice, i+1, std::forward<UArgs>(args)...);
- }
- template <typename T, typename... TArgs, typename... UArgs>
- T RandomChoiceNew(UArgs... args)
- {
- unsigned num = sizeof...(TArgs) + 1;
- unsigned choice = rand();
- choice %= num;
- return RandomChoiceNew_Recursive<T, TArgs...>(choice, 0, std::forward<UArgs>(args)...);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement