Concatenating strings (and numbers) in variadic template function
c++, c++11, variadic-functions, variadic-templates
Solution
inline std::string const& to_string(std::string const& s) { return s; }
template<typename... Args>
std::string stringer(Args const&... args)
{
std::string result;
using ::to_string;
using std::to_string;
int unpack[]{0, (result += to_string(args), 0)...};
static_cast<void>(unpack);
return result;
}
Problem
I am attempting to write a function that takes a variety of strings or numbers (that work with `std::to_string` and concatenate them. I've got it working with just strings, but I am having trouble with specializing depending on input as string or number. My code is called like this: `stringer("hello", "world", 2, 15, 0.2014, "goodbye", "world")` And here is what I've got: ``` inline std::string stringer(const std::string &string) { return string; } template <typename T, typename... Args> inline std::string stringer(const std::string &string, T &&val, Args &&...args) { return stringer(string+std::to_string(val), std::forward<Args>(args)...); } template <typename... Args> inline std::string stringer(const std::string &string, Args &&...args) { return stringer(string, std::forward<Args>(args)...); } ``` Currently it is breaking on any more than one string added unless the following are all numbers (due to the to_string). How can I specialize based on string or number to make the above work? Thanks.