From aebf74be971c11b8d8c1cc1edcbda82de0d0e984 Mon Sep 17 00:00:00 2001 From: Jose Luis Blanco-Claraco Date: Thu, 9 Nov 2023 00:13:46 +0100 Subject: [PATCH] FIX: MCP_SAVE() with class enums --- doc/source/doxygen-docs/changelog.md | 2 ++ .../containers/include/mrpt/containers/yaml.h | 22 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/doc/source/doxygen-docs/changelog.md b/doc/source/doxygen-docs/changelog.md index 10873745bb..4735991392 100644 --- a/doc/source/doxygen-docs/changelog.md +++ b/doc/source/doxygen-docs/changelog.md @@ -16,6 +16,8 @@ - mrpt::maps::TSetOfMetricMapInitializers::loadFromConfigFile() now throws if it finds a `*_count` entry with an unknown map class name. - \ref mrpt_math_grp - New template mrpt::math::confidenceIntervalsFromHistogram() +- BUG FIXES: + - Fix compilation errors if using the `MCP_SAVE()` macro with class enum types. # Version 2.11.2: Released Oct 25th, 2023 diff --git a/libs/containers/include/mrpt/containers/yaml.h b/libs/containers/include/mrpt/containers/yaml.h index 6ee0b8c371..c376ae8b67 100644 --- a/libs/containers/include/mrpt/containers/yaml.h +++ b/libs/containers/include/mrpt/containers/yaml.h @@ -912,6 +912,22 @@ std::ostream& operator<<(std::ostream& o, const yaml& p); MCP_LOAD_OPT(Yaml__, Var__); \ Var__ = mrpt::DEG2RAD(Var__) +namespace internal +{ +// We need to implement this as a template for the "if constexpr()" false branch +// not to be evaluated for enums, which would lead to build errors. +template +void impl_mcp_save(YAML_T& y, const T& var, const char* varName) +{ + using enum_t = std::remove_cv_t; + + if constexpr (std::is_enum_v) + y[varName] = mrpt::typemeta::TEnumType::value2name(var); + else + y[varName] = var; +} +} // namespace internal + /** Macro to store a variable into a mrpt::containers::yaml (initials MCP) * dictionary, using as "key" the name of the variable. * @@ -931,11 +947,7 @@ std::ostream& operator<<(std::ostream& o, const yaml& p); * values. Note that this requires enums to implement mrpt::typemeta::TEnumType. */ #define MCP_SAVE(Yaml__, Var__) \ - if constexpr (std::is_enum_v) \ - Yaml__[#Var__] = mrpt::typemeta::TEnumType< \ - std::remove_cv_t>::value2name(Var__); \ - else \ - Yaml__[#Var__] = Var__; + mrpt::containers::internal::impl_mcp_save(Yaml__, Var__, #Var__); #define MCP_SAVE_DEG(Yaml__, Var__) Yaml__[#Var__] = mrpt::RAD2DEG(Var__);