I have a handy function which splits a string into parts. The implementation is not important for my question.
inline std::vector<std::string> & split(const std::string & strInput, const std::string & strPattern, std::vector<std::string> & vec_strPart)
{...}
I want to make a templated version of this function in order to support wstrings and other kinds of strings. However, if I do this
template <class StringType>
inline std::vector<StringType> & tsplit(const StringType & strInput, const StringType & strPattern, std::vector<StringType> & vec_strPart)
{...}
then it does not work as expected
const std::string str("bla bla blaaaa");
const std::string strPattern(" ");
std::vector<std::string> vec_strPart;
split(str, strPattern, vec_strPart); // works
tsplit(str, strPattern, vec_strPart); // works
split(str, " ", vec_strPart); // works
tsplit(str, " ", vec_strPart); // does not work, StringType is ambiguous
tsplit(str, std::string(" "), vec_strPart); // works but changes client's code
My question is why using string constants does not work with the templated version while it works with the untemplated one. My assumption is that in the untemplated case, there is an implicit conversion from char[] to std::string while for templates, the matching occurs before implicit conversion.
How can I remedy the "ambiguity problem"? Can I maybe make a specialized version of tsplit which does the conversion to std::string and calls tsplit?