Similar to the this question. Needed throw a exception with a message using printf-like style instead of string cocatenation or iostreams. Using the C++ 20 Formatting Library:
throw std::runtime_error {
std::format("Critical error! Code {}: {}", errno, strerror(errno))
};
But it not feels ergonomic calling format in all exceptions with formatting, can it get better?
Yes it can!
Usage:
If you assemble the message with format in runtime you can use
std::vformat
. If needed locale you can add another constructor with it as first parameter. Note thatstd::format
can throw.EDIT: Barry comment, no need to move the format string and forward the arguments.