So I have some code written before C++11 that parses a string based on template arguments. Instead of having one definition for each number of arguments I would like to use variadic templates, but I can't wrap my head around how to initialize a tuple correctly. See this simplified code of what I want, this is for the special case of 2 arguments:
template <typename Arg1, typename Arg2>
struct parser
{
static tuple<Arg1, Arg2> parse(const string& str)
{
Arg1 arg1;
Arg2 arg2;
// do the parsing with for example stringstream
return tuple<Arg1, Arg2>(arg1, arg2);
}
};
I'm having problem with putting the arguments in the tuple in the variadic case. I can construct the return value holder with:
tuple<Args...> retVal;
but I don't know if there is a way to iterate through the arguments and put them in a tuple. I've seen some recursive magic to get for example the printf
functions, but I don't know if it could apply to this case.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…