Есть ли проблема с FMT format_as в C ++ 20?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Есть ли проблема с FMT format_as в C ++ 20?

Сообщение Anonymous »

У меня есть простой пример кода, который пытается использовать перегрузку format_as () для форматирования пользовательского типа (enum); Он выводит std :: string, найденную в карте. Переход на C ++ 17, он компилирует и работает правильно без других изменений. Я не имею смысла в этом сообщении или выходе от компилятора. Что мне не хватает?// Type your code here, or load an example.
#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:51: required from here
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:2868:40: in 'constexpr' expansion of 'fmt::v10::detail::parse_format_string(((fmt::v10::basic_format_string*)this)->fmt::v10::basic_format_string::str_, fmt::v10::detail::format_string_checker(fmt::v10::basic_string_view(((const char*)s))))'
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h:2597:44: in 'constexpr' expansion of 'fmt::v10::detail::parse_replacement_field((p + -1), end, (* & handler))'
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h:2573:35: in 'constexpr' expansion of '(& handler)->fmt::v10::detail::format_string_checker::on_replacement_field(adapter.fmt::v10::detail::parse_replacement_field(const char*, const char*, format_string_checker&)::id_adapter::arg_id, begin)'
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h:2740:20: in 'constexpr' expansion of '((fmt::v10::detail::format_string_checker*)this)->fmt::v10::detail::format_string_checker::on_format_specs(id, begin, begin)'
/opt/compiler-explorer/libs/fmt/11.0.0/include/fmt/base.h:2657:45: error: 'fmt::v10::detail::type_is_unformattable_for _' has incomplete type
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:74: required from 'void fmt::v10::print(format_string, T&& ...) [with T = {regtype_t&}; format_string = basic_format_string]'
2002 | return {{detail::make_arg

Подробнее здесь: https://stackoverflow.com/questions/796 ... -as-in-c20
Ответить

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

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

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

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

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