I am writing a TMP to count the number of elements passed to a struct
as template parameters using variadic templates. This is my code:
template<class T, T... t>
struct count;
template<class T, T h, T... t>
struct count<T, h, t...>{
static const int value = 1 + count<T, t...>::value;
};
template<class T>
struct count<T>{
static const int value = 0;
};
template<>
struct count<std::string, std::string h, std::string... l>{
static const int value = 1 + count<std::string, l...>::value;
};
template<>
struct count<std::string>{
static const int value = 0;
};
int main(){
std::cout << count<int, 10,22,33,44,56>::value << '\n';
std::cout << count<bool, true, false>::value << '\n';
std::cout << count<std::string, "some">::value << '\n';
return 0;
}
I get an error on the third instantiation of count
with std::string
because g++ 4.7
tells me error: ‘class std::basic_string<char>’ is not a valid type for a template non-type parameter
. Any workaround for this?