Geben Sie benutzerdefinierte FMT :: Formatierer für Protobuf -Nachrichtentypen anC++

Programme in C++. Entwicklerforum
Guest
 Geben Sie benutzerdefinierte FMT :: Formatierer für Protobuf -Nachrichtentypen an

Post by Guest »

Ich habe einige Protobuf -Nachrichtenklassen, die ich mit FMTLIB drucken können. Zum Beispiel habe ich eine TaskStatus -Nachricht, bei der TaskStatus von Google :: protobuf :: meldung abgeleitet wird. Ich verwende den folgenden Code, um dies zu tun: < /p>
template struct fmt::formatter : formatter
{
// parse is inherited from formatter

auto format(const google::protobuf::Message& message, format_context& ctx) const -> format_context::iterator
{
const auto result = fmt::format("{}", message.DebugString());
return formatter::format(result, ctx);
}
};
< /code>
Aber der Compiler scheint diesen Code nicht zu berücksichtigen. Wenn ich versuche, so diesen zu kompilieren: < /p>
void TaskServer::UpdateStatus(const TaskStatus& status)
{
fmt::print("TaskServer::UpdateStatus(): {}", status) // this is line 421
}
< /code>
Dann erhalte ich diese Fehlermeldungen: < /p>
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h: In instantiation of ?constexpr fmt::v9::detail::value fmt::v9::detail::make_value(T&&) [with Context = fmt::v9::basic_format_context; T = const TaskStatus&]?:
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:1777:29: required from ?constexpr fmt::v9::detail::value fmt::v9::detail::make_arg(T&&) [with bool IS_PACKED = true; Context = fmt::v9::basic_format_context; type = fmt::v9::detail::type::custom_type; T = const TaskStatus&; typename std::enable_if::type = 0]?
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:1901:77: required from ?constexpr fmt::v9::format_arg_store::format_arg_store(T&& ...) [with T = {const TaskStatus&}; Context = fmt::v9::basic_format_context; Args = {TaskStatus}]?
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:1918:31: required from ?constexpr fmt::v9::format_arg_store fmt::v9::make_format_args(Args&& ...) [with Context = basic_format_context; Args = {const TaskStatus&}]?
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:3294:44: required from ?void fmt::v9::print(format_string, T&& ...) [with T = {const TaskStatus&}; format_string = basic_format_string]?
PsDemo/PsDemo_Diagnostics/TaskServer.cpp:421:19: required from here
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:1757:7: error: static assertion failed: Cannot format an argument. To make type T formattable provide a formatter specialization: https://fmt.dev/latest/api.html#udt
1757 | formattable,
| ^~~~~~~~~~~
ThirdParty/spdlog/include/spdlog/fmt/bundled/core.h:1757:7: note: ?formattable? evaluates to false
< /code>
Wenn ich die fmt :: -Formatter -Vorlage ändere, um TaskStatus anstelle von Google :: protobuf :: meldung zu verwenden, dann funktioniert es einwandfrei. < /p>
template struct fmt::formatter : formatter
{
auto format(const TaskStatus& message, format_context& ctx) const -> format_context::iterator
< /code>
Aber ich habe viele verschiedene Klassen, die von Google :: protobuf :: meldung abgeleitet sind, die ich mit der einen Funktion verarbeiten möchte. Gibt es eine Möglichkeit, die Formatfunktion mit dem Argument von Google :: protobuf :: meldung zu erhalten, um für alle abgeleiteten Klassen zu arbeiten?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post