First, variadic templates don't include a way to say 'a variable number of arguments of a single type'. When you use variadic templates you get a parameter pack which is a set of zero or more arguments, each with a possibly unique type:
template<typename... Ts> void foo(Ts... ts);
the ...
token only has defined meanings for these parameter packs (and vararg functions, but that's beside the point). So you can't use it with non-parameter packs:
template<typename T> void foo(T... t); // error
template<typename T> void foo(T t...); // error
Second, once you have a parameter pack, you can't just iterate over the parameters the way you're showing with the range-based for-loop. Instead you have to write your algorithms in a functional style, using parameter pack expansion to 'peel off' parameters from the parameter pack.
// single argument base case
template<typename T>
void foo(T t) {
std::cout << t;
}
template<typename T,typename... Us>
void foo(T t,Us... us) {
foo(t) // handle first argument using single argument base case, foo(T t)
foo(us...); // 'recurse' with one less argument, until the parameter pack
// only has one argument, then overload resolution will select foo(T t)
}
Although variadic templates don't directly support what you want, you can use enable_if
and use the 'SFINAE' rule to impose this constraint. First here's a version that without the constraint:
#include <type_traits>
#include <utility>
template<class T>
T min(T t) {
return t;
}
template<class T,class... Us>
typename std::common_type<T,Us...>::type
min(T t,Us... us)
{
auto lowest = min(us...);
return t<lowest ? t : lowest;
}
int main() {
min(1,2,3);
}
And then apply enable_if
to ensure that the types are all the same.
template<class T,class... Us>
typename std::enable_if<
std::is_same<T,typename std::common_type<Us...>::type>::value,
T>::type
min(T t,Us... us)
{
auto lowest = min(us...);
return t<lowest ? t : lowest;
}
The modified implementation above will prevent the function from being used any time the arguments aren't all exactly the same according to is_same
.
You're probably better of not using these tricks if you don't have to. Using an initializer_list as KennyTM's suggests is probably a better idea. In fact if you're really implementing min and max then you can save yourself the trouble because the standard library already includes overloads that take an initializer_list.
How does is_same<T,typename common_type<Us...>::type>
work?
Because there's a single argument version of min()
the variadic version is selected only when there are two or more parameters. This means that sizeof...(Us)
is at least one. In the case where it is exactly one, common_type<Us...>
returns that type single type, and is_same<T,common_type<Us...>>
ensures that the two types are the same.
The variadic implementation of min()
calls min(us...)
. So long as this call only works when all the types in Us...
are the same we know that commont_type<Us...>
tells up what that type is, and is_same<T,common_type<Us...>>
ensures that T is also that same type.
So we know that min(a,b)
only works if a
and b
are the same type. And we know that min(c,a,b)
calls min(a,b)
so min(c,a,b)
can only be called if a
and b
are the same type and additionally if c
is also the same type. min(d,c,a,b)
calls min(c,a,b)
so we know that min(d,c,a,b)
can only be called if c
, a
, and b
are all the same type, and additionally if d
is also the same type. Etc.
...
s are just a pseudocode way of saying "more stuff here").