Hello, people. How is your day?
Can I get an aid with my problem? I want compile-time checks of parameters correctness (number of param mismatch etc.) with syntax like fmt::format("The answer is {:d}", "forty-two");
. Is that possible? From website I only see alternative format(fmt
kind of syntax that switches to compile time checks instead of throwing exceptions.
I tried DCMAKE_FMT_PEDANTIC=ON
didn't seem to help on both gcc and clang, probably it's doing not what I thought it would.
Reason to do this is to have compile-time checks for another library that uses fmt
(named spdlog
) while not changing anything on spdlog side...
I promise I did research myself prior to asking here, sorry for any trouble. I hope someone else had solution for this, spdlog seems to be quite popular and is mentioned in readme as a fellow library.
fmt
(or FMT_STRING
) macro as described in http://fmtlib.net/latest/api.html#compile-time-format-string-checks
template <>
struct formatter<Polygon> {
template <typename ParseContext>
constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const Polygon &p,
FormatContext &ctx)
{
fmt::memory_buffer out;
bool is_first = true;
for (auto& vertice : p.vertices)
{
if(is_first)
{
format_to(out, "{}", vertice);
is_first = false;
}
else
{
format_to(out, ", {}", vertice);
}
}
return format_to(ctx.begin(), "{}", to_string(out));
}
};
}
So something like:
template <typename FormatContext>
auto format(const Polygon &p,
FormatContext &ctx)
{
//fmt::memory_buffer out;
bool is_first = true;
for (auto& vertice : p.vertices)
{
if(is_first)
{
format_to(ctx.begin(), "{}", vertice);
is_first = false;
}
else
{
format_to(ctx.begin(), ", {}", vertice);
}
}
return ctx.begin();
}
Seems much better. Thanks!
So something like
template <typename FormatContext>
auto format(const :Polygon &p,
FormatContext &ctx)
{
bool is_first = true;
for (auto& vertice : p.vertices)
{
if(is_first)
{
format_to(ctx.begin(), "{}", vertice);
is_first = false;
}
else
{
format_to(ctx.begin(), ", {}", vertice);
}
}
return ctx.begin();
}
seems much better. Thanks!
format_to
and use out()
instead of begin()
(the latter is deprecated): template <typename FormatContext>
auto format(const :Polygon &p,
FormatContext &ctx)
{
bool is_first = true;
auto out = ctx.out();
for (auto& vertice : p.vertices)
{
if(is_first)
{
out = format_to(out, "{}", vertice);
is_first = false;
}
else
{
out = format_to(out, ", {}", vertice);
}
}
return out;
}
hello i tested fmt::snprintf on vs 2017 like this:
The first one does only remove %s and %d resulting in
hallo mein name ist : und ich bin jahre altÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
The other two replace the format specifier correctly with their value.
Also neither of them does a null termination?
Am i missing something ? shouldnt fmt::snprintf work exactly like std::snprintf?
MachineInstr
. I am receiving this error (since I'm using C++11): function uses ‘auto’ type specifier without trailing return type
. Which return type should I put instead of auto?
template <> struct fmt::formatter<llvm::MachineInstr> {
template <typename ParseContext> constexpr auto parse(ParseContext &ctx) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const llvm::MachineInstr &MI, FormatContext &ctx) {
return fmt::format_to(ctx.begin(), "{}", to_string(MI));
}
};
fmt::print("{}", str);
or fmt::print(str);
. For example: Print("({:.1f}, {:.1f})");
works, but it could be a hack as this is not explicitly documented. Furthermore, if both are valid, which one is the most performant and thus recommended?
fmt::print("({:.1f}, {:.1f})")
won't work: https://godbolt.org/z/YqpUyh
typename FormatContext::iterator
there.
fmt::formatter<std::wstring_view, char>
and fmt::formatter<std::string_view, wchar_t>
, and more formatters to handle std::wstring
, std::string
, const wchar_t*
, and const char*
. But I'm getting a static assertion failure "mixing character types is disallowed". Is there any way to do this besides converting the original string before formatting or the resulting string after formatting?
__PRETTY_FUNCTION__
macro).
fmt::print(stderr, "{}", to_string(buf));
I've been using fmt while (as part of spdlog) for a while and think it's absolutely fantastic (like seriously--while concepts and coroutines are great and all, I think fmt getting into c++20 is just as big of a deal).
One mistake that seems to keep biting us is format string errors of the form:fmt::format("missing format: ", number)
i.e. the number of {}
in the format string doesn't match the number of supplied arguments. I was surprised the first time I saw this that statements like this compile fine without warning. Is there a way to get errors like this to throw a compile time error?