I would like to enforce the type of variadic template to be identical to an earlier set template type. In the below example, I\'d like T and U to be the same type.
c
We can use SFINAE to ensure that all U
types are the same as T
. An important thing to note is that U
is not just one type as you imply, but a list of possibly disparate types.
template::value>* = nullptr>
Foo(T first, U... vals) {
std::cout << "Called multiple argument ctor" << std::endl;
// [...]
}
std::enable_if_t
is from C++14. If that's not an option for you, just use std::enable_if
.
typename std::enable_if::value>::type* = nullptr>
all_same
can be implemented in a bunch of different ways. Here's a method I like using boolean packs:
namespace detail
{
template struct bool_pack;
template
//if any are false, they'll be shifted in the second version, so types won't match
using all_true = std::is_same, bool_pack>;
}
template
using all_true = detail::all_true;
template
using all_same = all_true...>;