I am trying to use spdlog
. I incorporated it with my code, but now I am getting the following error:
....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]
]
... and this is the end of the error message. It never reaches my code, so I have no idea where to look at. Any ideas what could be the reason for this?
spdlog is version 1.6.1. The last error line is from here:
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
}
}
and it doesn't look particularly wrong to me.
Update: after some try-and-errors commenting out all spdlog
invocations I narrowed it down to this:
spdlog::info("Foo{}", Point{1, 2});
Where Point
is my own class in my own namespace. I do provide for it a way to print itself:
template<typename OStream>
OStream &operator<<(OStream &os, const Point &p) {
return os << "[" << p.x << ", " << p.y << "]";
}
spdlog uses the {fmt}
library for formatting the output text, and that requires the <fmt/ostream.h>
header file to be included in order to enable std::ostream
-like support.
Regardless of using external implementation of the {fmt}
library, or the one from spdlog sources itself, there's the special header <spdlog/fmt/ostr.h>
that includes the file version that is in use.
Once included, spdlog should be able to use your operator<<
.
Alternatively, you can create a custom formatter, that's also capable of parsing a formatting string:
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);
}
};