How to use variadic templates and template metaprogramming to accept different type as input?
//This template takes variable number of c++ char and add'\0'
//to the end to mimic the c_str() inbuilt function
template<char ...Cs> struct listChar
{
constexpr std::array<char, 1 + sizeof...(Cs)> cTypeStr()
{
return { {Cs..., '\0'} };
}
};
int main(int argc, char** argv)
{
auto temp1 = listChar<'C','H','A','N','G','E'>::cTypeStr();//This works
//auto temp2 = listChar<"Change">::cTypeStr(); // Do not work
//auto temp3 = listChar<"Change",'C','H','A','N','G','E'>::cTypeStr();//Do not work
cout << "Output: \n" << spStr.data();
getchar();
return 0;
}
If you don't need to build the string at compile time, here's a much simpler approach that doesn't involve variadic templates or type traits: