I'd like to run following code:
#include <iostream>
#include <type_traits>
template<class ... T, class T1, bool B = std::is_integral<T1>::value>
void printIntegrals(T1 thing, T ... stuff) {
if(B)
std::cout<<"is integral!"<<std::endl;
else
std::cout<<"is not integral!"<<std::endl;
printIntegrals(stuff ...);
}
template<class T1>
void printIntegrals<{},float>(T1 thing, T ... stuff) {
std::cout<<"is not integral!"<<std::endl;
}
int main() {
bool b;
int i;
double d;
char c;
float f;
printIntegrals(b, i, d, c, f);
}
However, the specialized template doesn't get initialized. In the compiler error, it is written:
main.cpp:16:6: error: template-id 'printIntegrals<<expression error> >' used as a declarator
which means I'm having a syntactic problem. Is this because the empty initializer list isn't a correct special value for the parameter pack? The idea to create the specialization in this way seems to be correct as the compiler cannot instantiate the following function:
main.cpp: In instantiation of 'void printIntegrals(T1, T ...) [with T = {}; T1 = float; bool B = false]' ...
Update:
The error stem from my confusion with respect to template class specialization and function overloading. Thanks to the comments, I could create this running example:
#include <iostream>
#include <type_traits>
template<class T1, bool B = std::is_integral<T1>::value> void printIntegrals(T1 thing) {
if(B)
std::cout<<"is integral!"<<std::endl;
else
std::cout<<"is not integral!"<<std::endl;
}
template<class ... T, class T1, bool B = std::is_integral<T1>::value>
void printIntegrals(T1 thing, T ... stuff) {
if(B)
std::cout<<"is integral!"<<std::endl;
else
std::cout<<"is not integral!"<<std::endl;
printIntegrals(stuff ...);
}
int main() {
bool b{true};
int i{0};
double d{.1};
char c{'a'};
float f{.1};
printIntegrals(b, i, d, c, f);
}