Paste: concat dat

Author: erikc
Mode: c++
Date: Thu, 13 Jan 2011 04:07:46
Plain Text |
size_t sum(size_t n) { return n; }
template <typename... Args>
size_t sum(size_t n, Args&&... args) {
  return n + sum(std::forward<Args...>(args...));
}

void concat_(std::string& a, const std::string& s) {
  a += s;
}

template <typename... Args>
void concat_(std::string& a, const std::string& s, Args&&... args) {
  a += s;
  concat_(s, std::forward<Args...>(args...));
}

template <typename... Args>
std::string concat(Args&&... args) {
  std::string result;
  result.reserve(sum(args.size()...));
  concat_(result, std::forward<Args...>(args...));
  return result;
}

New Annotation

Summary:
Author:
Mode:
Body: