I would like to use C++11's variadic templates to achieve a generalized "random picker" function.
Something like this...
template <typename T>
T randomPicker(T one, T two, T three)
{
int pick = 3 * (rand() / double(RAND_MAX));
switch (pick)
{
case 0:
return one;
case 1:
return two;
default:
return three;
}
}
... except generalized to accept any number of parameters (each of the same type, as above -- although accepting any type as a parameter and converting the chosen one to some specific type T upon return would be acceptable also).
I understand the idea of using template recursion to achieve things like the typesafe printf, etc. Can variadic templates also be used to create the sort of function described above? Any tips appreciated!