Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

build tuple using variadic templates

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.

like image 530
Rolle Avatar asked Apr 04 '12 15:04

Rolle


1 Answers

You don't need a helper class. Do it with functions instead.

template <typename T> std::tuple<T> parse(std::istream& is) 
{
  T t; is >> t;
  return std::tuple<T>(std::move(t));
}

template <typename T, typename Arg, typename... Args>
std::tuple<T, Arg, Args...> parse(std::istream& is) 
{
  T t; is >> t;
  return std::tuple_cat(std::tuple<T>(std::move(t)),
                        parse<Arg, Args...>(is));
}

template <typename... Args>
std::tuple<Args...> parse(const std::string& str) 
{
  std::istringstream is(str);
  return parse<Args...>(is);
}

EDIT: Today, I got the idea how to do it in a very simple way with the use of the expansion:

template <typename T> T read(std::istream& is)
{
  T t; is >> t; return t;
}

template <typename... Args>
std::tuple<Args...> parse(std::istream& is) 
{
  return std::make_tuple(read<Args>(is)...);
}

template <typename... Args>
std::tuple<Args...> parse(const std::string& str) 
{
  std::istringstream is(str);
  return parse<Args...>(is);
}
like image 91
ipc Avatar answered Oct 04 '22 09:10

ipc