См. следующий код:
Код: Выделить всё
#include
#include
#include
#include
class exception_t
: public std::exception
{
public:
template
exception_t(std::string_view users_fmt, args_t&&... args)
: m_users_fmt(users_fmt)
, m_format_args(std::move(std::make_format_args(args...)))
{}
char const* what() const noexcept override
{
thread_local static std::string s = std::vformat(m_users_fmt, m_format_args);
return s.c_str();
}
private:
std::string m_users_fmt;
std::format_args m_format_args;
};
int main()
try
{
throw exception_t("{}", 42);
}
catch (std::exception& e)
{
std::println("{}", e.what());
}
What am I doing wrong here?
=================================================
Update after response of HolyBlackCat
Above is a minimal code snippet, but it is the intention to completely separate the formatting from exception/error/warning reporting. I'd like to store a standard type with the arguments e.g. on disk. And later another process reads it from disk and displays the data using a format string of the local language. That's why I don't want to call std::vformat in the constructor in this code snippet.
Does any alternative for std::format_args exist? E.g. can I store args?
Источник: https://stackoverflow.com/questions/781 ... ormat-args