Hacker News new | ask | show | jobs
by puffybuf 821 days ago
I did something similar as a student, making my own exception class with std::ostream:

throw exceptionC() << "error code: " << t;

I often found myself having to format error strings for exceptions, so I thought I could just do it like cout in one line. I know now this is bad for i18n strings.

2 comments

Why would you prefer that over:

  throw exceptionC("error code: ", t);
?

Ever since perfect forwarding I've been using this pattern to get out of arrow hell:

  template <typename Arg, typename...Args>
  string to_string(Arg &&arg, Args &&...args) {
    stringstream buf;
    buf << arg;
    ((buf << std::forward<Args>(args)), ...);
    return buf.str();
  }