Hacker News new | ask | show | jobs
by shadedtriangle 842 days ago
I feel like this article is missing one of the most useful idioms of parameter packs which is when you want to accept a variety of parameter types to a non-template function. You can stuff a parameter pack into an array/vector of unions/variants which is quite useful! For example it's the way to implement std::format which is based off Python's string.format().

  using FormatArg = variant<int, float, string>;
  string FormatArgs(const char* fmt, const vector<FormatArg> &args);
  
  template <typename... Args>
  string Format(const char* fmt, Args&&... args) {
    vector<FormatArg> expanded_args = {args...};
    return FormatArgs(fmt, expanded_args);
  }

  int main() {
    cout << Format("Hello {} pi {}", "world", 3.14f);
  }
https://godbolt.org/z/v5zo99aGe
1 comments

What is your non-template function in this example?