I just learned about variadic templates in C++. I implemented it, but I want to know, can it do the following?
If I want a function with a variable number of arguments, I could do that:
template <typename... Ts>
f(Ts... args);
But I lose type safety (I don't know the type of the arguments).
What if I know my function needs only float as arguments? I want to make sure at compile-time that every argument is the type I want.
So these are my questions:
Is there a way to force a certain type with variadic templates (something like this)?
template <float... Fs> f(Fs... args); // unlimited number of arguments but only floatIf not, is there a way to check it at compile-time?
static_assert(std::is_same<A,B>)is fine in most cases, but it doesn't work for templated classes (like for my use case):template <typename T, uint16_t dimension> class Vector { template <typename... Ts> Vector(Ts... args) { static_assert(sizeof...(args) == dimension); static_assert(std::is_same_v<Ts..., T>()); //doesn't work because Ts will //develop into a lot of template //arguments. Just putting Ts doesn't //work either. } }
Ps: Yes I could use std::vector or std::array as arguments, but that's not really the point. Plus, I want to keep the beautiful Vector(2.0, 1.0, 0.0) syntax, not using curly braces.