I want to write a function that will do an operation based on the types, not arguments, of a function. As such, the function receives no template based arguments. The general gist is something like this:
#include <iostream>
void func() {
std::cout<<"End marker\n";
}
template <typename Type, typename... T>
void func() {
std::cout<<"Type sizeof "<<sizeof(T)<<"\n";
func<T...>();
}
int main() {
func<int, int, int>();
}
Which, of course, doesn't compile. I've tried doing this:
template <typename Type, typename... T>
void func() {
std::cout<<"Type sizeof "<<sizeof(T)<<"\n";
if( sizeof...(T)!=0 )
func<T...>();
}
This does not work, however. The func<T...>
may not get evaluated, but it does need to be compilable.
Is there a way to do this that I'm missing?
You can make your original setup work by making func
a "template function" (that doesn't actually use the template) like:
template<int = 0>
void func() {
std::cout<<"End marker\n";
}
template <typename Type, typename... T>
void func() {
std::cout<<"Type sizeof "<<sizeof(Type)<<"\n";
func<T...>();
}
And your second one can work by using if constexpr
, so func<>()
isn't compiled.