Skip to content

Instantly share code, notes, and snippets.

@RicoP
Created February 23, 2021 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RicoP/b000585f4c2fe4d3d51e12b9c6586a38 to your computer and use it in GitHub Desktop.
Save RicoP/b000585f4c2fe4d3d51e12b9c6586a38 to your computer and use it in GitHub Desktop.
Get random template paramter
#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