Just wondering why is this invalid:
#include <iostream>
template <std::size_t... Is>
void foo(Is&&... args) {
std::cout << "foo called with " << sizeof...(Is) << "params\n";
}
int main() {
foo(1, 2, 3, 4);
}
It seems a perfectly reasonable example, yet it fails on any compiler I can get my hands on.
If I substitute size_t
for class
the example works as expected. I've also tried using the new auto
template parameter but no online compiler accepts this so I don't know if this an invalid use case or a conformance issue.
It's not valid C++, that's why.
If you instantiate that function template with the template arguments 1, 2, 3, 4
then after substituting the arguments into the template you get the signature:
void foo(1&&, 2&&, 3&&, 4&&);
That's clearly not a valid function.
If you want to write a function template that accepts any number of arguments but only if they are of the right type, you can do that like this in C++17:
template<typename T>
using is_size_t = std::is_same<T, std::size_t>;
template<typename... T>
std::enable_if_t<std::conjunction<is_size_t<T>...>::value>>
foo(T&&... args);
Or alternatively (also using C++17):
template<typename... T>
std::enable_if_t<(std::is_same_v<std::size_t, T> && ...)>
foo(T&&... args);
For C++14 you need to implement std::conjunction
yourself, e.g. using the and_
template from p0032r1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With