#include
#include
#include
template
std::map create_reverse_map(const std::map& input_map);
typedef enum {
RT_COIL = 0,
RT_INPUT = 1,
RT_HOLDING = 2
} regtype_t;
auto format_as(regtype_t rt);
//Make a map associating strings with enum values,
//and a reverse map we'll generate at runtime
static std::map regtypes = {
{"COIL", RT_COIL},
{"INPUT", RT_INPUT},
{"HOLDING", RT_HOLDING}
};
static std::map regtypes_r;
int main(void) {
//create a reverse of our lookup map, for diagnostic message output
regtypes_r = create_reverse_map(regtypes);
//Try format of a regtype_t enum, pulled from the lookup map
//Should get us the same string we're using for the key here
fmt::print("Testing: '{0}'\n", regtypes["HOLDING"]);
return 0;
}
// format_as() overload for our enum
// tack on some asterisks so we can see we've been here
auto format_as(regtype_t rt) { return std::string("*** ") + regtypes_r[rt]; }
// reverse map generator
template
std::map create_reverse_map(const std::map& input_map) {
std::map reverse_map;
for (const auto& pair : input_map) {
// both keys and values must be unique
assert (reverse_map.find(pair.second) == reverse_map.end());
reverse_map[pair.second] = pair.first;
}
return reverse_map;
}
< /code>
Ошибка компиляции в C ++ 20: < /p>
In file included from /opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/format.h:41,
from :2:
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h: In instantiation of 'constexpr decltype (ctx.begin()) fmt::v10::detail::parse_format_specs(ParseContext&) [with T = regtype_t; ParseContext = compile_parse_context; decltype (ctx.begin()) = const char*]':
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
2747 | return id >= 0 && id < num_args ? parse_funcs_[id](context_) : begin;
| ~~~~~~~~~~~~^
:31:16: in 'constexpr' expansion of 'fmt::v10::basic_format_string("Testing: \'{0}\'\012")'
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
2657 | type_is_unformattable_for _;
| ^
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h: In instantiation of 'constexpr fmt::v10::detail::value fmt::v10::detail::make_arg(T&) [with bool PACKED = true; Context = fmt::v10::context; T = regtype_t; typename std::enable_if::type = 0]':
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h
2002 | return {{detail::make_arg
Подробнее здесь: https://stackoverflow.com/questions/796 ... -as-in-c20
Мобильная версия