dimanche 19 avril 2015

C++ templates: Variadic template for creating formated string

I want to create a formating method that with given typename arguments generates a string. I'm using a variadic template for this:



template<typename T>
std::string GetFormat()
{
std::string ret;
if (typeid(T) == int)
ret = "i";
else if (typeid(T) == float)
ret = "f";
else
ret = "n";

return ret;
}

template<typename... Args>
std::string GetFormatVArgs()
{
std::string ret;
// for each arg in Args
// ret += GetFormat<arg>()
return ret;
}

void main()
{
std::string str = GetFormatVArgs<float, float, int>();
std::cout << str;
}


Expected output:


ffi


How could I iterate for each typename and give it to GetFormat()?


Aucun commentaire:

Enregistrer un commentaire