Пожизненная проблема при хранении std::format_argsC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Гость
 Пожизненная проблема при хранении std::format_args

Сообщение Гость »


См. следующий код:

Код: Выделить всё

#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());
}
When debugging this under MSVC 2022 v143 with SDK 10.0.19041.0, I see that during the construction of exception_t m_format_args has an appropriate value, but at the time of calling what(), m_format_args has an invalid value. Most likely this is a lifetime issue, but I cannot see what is going wrong. (Most often this program does not print 42)
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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • C++: Шаблон для std::format списка объектов, доступных std::format()?
    Anonymous » » в форуме C++
    0 Ответы
    49 Просмотры
    Последнее сообщение Anonymous
  • C++: Шаблон для std::format списка объектов, доступных std::format()?
    Anonymous » » в форуме C++
    0 Ответы
    30 Просмотры
    Последнее сообщение Anonymous
  • Шаблон для std::format списка объектов, доступных std::format()? [закрыто]
    Anonymous » » в форуме C++
    0 Ответы
    36 Просмотры
    Последнее сообщение Anonymous
  • Пожизненная проблема с std::format_args
    Anonymous » » в форуме C++
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Пожизненная проблема с std::format_args
    Гость » » в форуме C++
    0 Ответы
    16 Просмотры
    Последнее сообщение Гость

Вернуться в «C++»