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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…