I'm trying to write a function that takes as an input argument a string, three output arguments that can be of varying types (at least that's the idea) and a char that is a delimiter.
The input string is delimited by the specified char and the function assigns each char-terminated field to each of the output arguments in order (at the moment it takes care of input strings such as "a;bb;ccc" and is limited to three output arguments only but that's not the problem).
For example with an input such as 10;200;3000 I'd get 10, 200 and 3000 for the first, second and third output arguments respectively
The output arguments need to be of either string or integer types, but due to my limited knowledge of C++ (and in particular generic programming) I'm having trouble writing a generic function that doesn't care about that.
I have the following code:
template <typename T>
void parse_string (std::string &input, T &out1, T &out2, T &out3, const char delimiter)
{
while (out3 == "")
{
std::string temp = input.substr(0, input.find(delimiter));
input = input.substr(input.find(delimiter) +1);
if (out1 == "") { out1 = temp;}
else if (out2 == "") { out2 = temp;}
else { out3 = temp;}
}
}
and it works fine for strings but obviously not for integers.
I suspect I'm going wrong in the bits where I check if the argument is empty (among other parts I don't know about).
Could you please help me improve it?
Also, I would welcome any ideas on improving the logic itself (perhaps I'd need to go with the variadic templates to make the number of arguments flexible, but I'd have to check with our technical lead if C++11 standards are okay).
Ideally I'd like to avoid the situation where I have the exact same function twice but with a different signature for each of the types (one for strings and one for ints).
Many thanks in advance!