升级至fmt v10及以上版本后枚举类型格式化编译失败
fmt v10及以上版本枚举类型编译失败问题
以下代码在fmt v9.1版本可正常编译,但升级至fmt v10.0及以上版本时编译失败,这是fmt v10+的既定行为。
代码示例
#include <iostream> #include <fmt/core.h> #include <fmt/ostream.h> using Test = enum Test_e{ A,B,C }; int main(int, char **) { fmt::print(std::cout, "test enum {}\n", A); }
升级至fmt v10.0后的错误信息(C++11环境)
In file included from <source>:2: /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h: In instantiation of 'fmt::v10::detail::value<Context> fmt::v10::detail::make_value(T&&) [with Context = fmt::v10::basic_format_context<fmt::v10::appender, char>; T = Test_e&]': /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h:1711:29: required from 'fmt::v10::detail::value<Context> fmt::v10::detail::make_arg(T&&) [with bool IS_PACKED = true; Context = fmt::v10::basic_format_context<fmt::v10::appender, char>; type <anonymous> = fmt::v10::detail::type::custom_type; T = Test_e&;; typename std::enable_if<IS_PACKED, int>::type <anonymous> = 0]' /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h:1827:77: required from 'fmt::v10::format_arg_store<Context, Args>::format_arg_store(T&& ...) [with T = {Test_e&}; Context = fmt::v10::basic_format_context<fmt::v10::appender, char>; Args = {Test_e}]' /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h:1844:31: required from 'constexpr fmt::v10::format_arg_store<Context, typename std::remove_cv<typename std::remove_reference<T>::type>::type ...> fmt::v10::make_format_args(T&& ...) [with Context = basic_format_context<appender, char>; T = {Test_e&}]' /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/ostream.h:179:44: required from 'void fmt::v10::print(std::ostream&, format_string<T ...>, T&& ...) [with T = {Test_e}; std::ostream = std::basic_ostream<char>; format_string<T ...> = basic_format_string<char, Test_e>]' <source>:10:15: required from here /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h:1691:7: error: static assertion failed: Cannot format an argument. To make type T formattable provide a formatter<T> specialization: https://fmt.dev/latest/api.html#udt 1691 | formattable, | ^~~~~~~~~~~ /opt/compiler-explorer/libs/fmt/10.0.0/include/fmt/core.h:1691:7: note: 'formattable' evaluates to false Compiler returned: 1
相关说明
- 该问题属于fmt v10+的既定行为,相关GitHub问题编号为3720、3671
- 有两个Godbolt在线演示案例(感谢@Marek R提供)
内容的提问来源于stack exchange,提问作者Louis Go
相关产品推荐
相关产品推荐

