Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...