A variadic function that accepts Strings and Ints, Format the latter and concatenate all?

  • A+
Category:Languages

I'm trying to use the answer from DanielKO in this question for my needs but i'm not familiar with templates and variadic functions, and i don't get what should i do.

What i'd need is a variadic c++(11) function which i can call like this:

 String NewMsg = CreateMessage("SET",16,1,17,0,"RED",47); 

and have NewMsg= "SET,0010,0001,0011,0000,RED,002F".

I'm not even able to get where should i add the comma between the arguments. And then: How could i distinguish between integers and string while parsing the args, so to format each integer to hexadecimal strings?

 


You use recursion and function overloading

std::string CreateMessage(int i) {     return /* i formatted as hex */; }  std::string CreateMessage(const char* s) {     return s; }  template<typename T, typename... Ts> std::string CreateMessage(T t, Ts... ts) {     return CreateMessage(t) + "," + CreateMessage(ts...); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: