我正在尝试使用spdlog
. 我将其合并到我的代码中,但现在出现以下错误:
....fmt\core.h(1016): error C2338: Cannot format argument. To make type T formattable provide a formatter<T> specialization: https://fmt.dev/latest/api.html#formatting-user-defined-types
....fmt/core.h(1013): note: while compiling class template member function 'int fmt::v6::internal::arg_mapper<Context>::map(...)'
with
[
Context=context
]
....fmt/core.h(1213): note: see reference to function template instantiation 'int fmt::v6::internal::arg_mapper<Context>::map(...)' being compiled
with
[
Context=context
]
....fmt/core.h(1027): note: see reference to class template instantiation 'fmt::v6::internal::arg_mapper<Context>' being compiled
with
[
Context=context
]
....fmt/core.h(1198): note: see reference to alias template instantiation 'fmt::v6::internal::mapped_type_constant<int,context>' being compiled
....fmt/core.h(1342): note: see reference to function template instantiation 'unsigned __int64 fmt::v6::internal::encode_types<Context,int,>(void)' being compiled
with
[
Context=context
]
....fmt/format.h(3375): note: see reference to class template instantiation 'fmt::v6::format_arg_store<context,int>' being compiled
....spdlog/details/fmt_helper.h(49): note: see reference to function template instantiation 'std::back_insert_iterator<fmt::v6::internal::buffer<char>> fmt::v6::format_to<char[6],int&,250,char>(fmt::v6::basic_memory_buffer<char,250,std::allocator<char>> &,const S (&),int &)' being compiled
with
[
S=char [6]
]
Run Code Online (Sandbox Code Playgroud)
...这是错误消息的结尾。它永远不会到达我的代码,所以我不知道该看哪里。有什么想法可能是什么原因吗?
spdlog 的版本是 1.6.1。最后一个错误行来自这里:
inline void pad2(int n, memory_buf_t &dest)
{
if (n >= 0 && n < 100) // 0-99
{
dest.push_back(static_cast<char>('0' + n / 10));
dest.push_back(static_cast<char>('0' + n % 10));
}
else // unlikely, but just in case, let fmt deal with it
{
fmt::format_to(dest, "{:02}", n); // <--- HERE
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这看起来并不是特别错误。
更新:在注释掉所有调用之后,spdlog
我将其范围缩小到:
spdlog::info("Foo{}", Point{1, 2});
我自己的类在我自己的命名空间中的哪里Point
。我确实为它提供了一种打印自身的方法:
template<typename OStream>
OStream &operator<<(OStream &os, const Point &p) {
return os << "[" << p.x << ", " << p.y << "]";
}
Run Code Online (Sandbox Code Playgroud)
spdlog使用该{fmt}
库来格式化输出文本,并且需要包含<fmt/ostream.h>
头文件才能启用类似支持。std::ostream
无论使用{fmt}
库的外部实现,还是来自 spdlog 源本身的库,都有一个特殊的标头<spdlog/fmt/ostr.h>
,其中包含正在使用的文件版本。
一旦包含在内,spdlog 应该能够使用您的operator<<
.
或者,您可以创建一个自定义格式化程序,它也能够解析格式化字符串:
template <>
struct fmt::formatter<Point> {
constexpr auto parse(format_parse_context& ctx) {
return ctx.end();
}
template <typename Context>
auto format(const Point& p, Context& ctx) {
return format_to(ctx.out(), "[{}, {}]", p.x, p.y);
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5700 次 |
最近记录: |