I am creating a variation template.
Let's say I have something like this:
template<typename T, T ... Numbers> class Sequence final {
Instances of this class can be created like this:
Sequence<uint32_t, 1, 2, 3, 42, 25> seq;
I would like to make sure that during compilation using static_assert , the numbers parameter package contains only certain numbers. For this example, suppose I only want to allow 0 or 1 .
So, I would like to do something like:
for (size_t i = 0; i < count; i++) { static_assert(numbers[i] == 1 || numbers[i] == 0, "Only ones and zeroes are allowed."); }
But obviously static_assert does not work with a for loop. I am sure there must be some syntax for this, but I could not figure it out.
I would prefer to use something that compiles with the C ++ 11 compiler (or perhaps the C ++ 14 compiler if it is not executable in C ++ 11).
c ++ c ++ 11 static-assert variadic-templates
Venemo
source share