Created
February 23, 2021 11:15
-
-
Save RicoP/b000585f4c2fe4d3d51e12b9c6586a38 to your computer and use it in GitHub Desktop.
Get random template paramter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <ctime> | |
template<typename Head, typename... Tail> | |
constexpr int count_template_parameter(const Head &) { | |
return 1; | |
} | |
template<typename Head, typename... Tail> | |
constexpr int count_template_parameter(const Head &, const Tail & ...tail) { | |
return 1 + count_template_parameter(tail...); | |
} | |
template<typename Head, typename... Tail> | |
const Head & get_template_parameter(int i, const Head & h) { | |
return h; | |
} | |
template<typename Head, typename... Tail> | |
const Head & get_template_parameter(int i, const Head & head, const Tail & ...tail) { | |
if(i == 0) return head; | |
return get_template_parameter(i-1, tail...); | |
} | |
template<typename... Vargs> | |
const auto & random_template_parameter(const Vargs & ...args) { | |
int i = rand() % (count_template_parameter(args...)); | |
return get_template_parameter(i, args...); | |
} | |
int main() { | |
srand (time(NULL)); | |
return random_template_parameter(1,114,42,7,8,6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment