Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatter for std::atomic #3574

Merged
merged 1 commit into from
Aug 14, 2023
Merged

Add formatter for std::atomic #3574

merged 1 commit into from
Aug 14, 2023

Conversation

wangzw
Copy link
Contributor

@wangzw wangzw commented Aug 6, 2023

There is no formatter for std::atomic and std:errc and inject format_as in std namespace is not a good idea.

This patch implement formatter for above types.

include/fmt/std.h Outdated Show resolved Hide resolved
@vitaut
Copy link
Contributor

vitaut commented Aug 7, 2023

You need to include <atomic> in fmt/std.h.

@wangzw
Copy link
Contributor Author

wangzw commented Aug 8, 2023

You need to include <atomic> in fmt/std.h.

Fixed and passed all tests

@wangzw wangzw requested a review from phprus August 10, 2023 13:08
include/fmt/std.h Outdated Show resolved Hide resolved
Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

include/fmt/std.h Outdated Show resolved Hide resolved
Comment on lines 450 to 463
FMT_EXPORT
template <typename Char>
struct formatter<std::errc, Char>
: formatter<std::underlying_type<std::errc>::type, Char> {
template <typename FormatContext>
FMT_CONSTEXPR auto format(std::errc v, FormatContext& ctx) const
-> decltype(ctx.out()) {
using underlying_type = std::underlying_type<std::errc>::type;
return formatter<underlying_type, Char>::format(
static_cast<underlying_type>(v), ctx);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if providing a formatter for errc is a good idea. I think it should be up to the user to decide how they want to present it and if they want a number they could use to_underlying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to provide a default formatter if no other formatter exists?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that it is easy to format errc as a number with to_underlying, I don't think it's worth exploring the default formatter option.

@wangzw wangzw changed the title Add formatter for std::atomic and std:errc Add formatter for std::atomic Aug 14, 2023
@vitaut vitaut merged commit 5a866fe into fmtlib:master Aug 14, 2023
40 checks passed
@vitaut
Copy link
Contributor

vitaut commented Aug 14, 2023

Thank you!

@vitaut
Copy link
Contributor

vitaut commented Aug 14, 2023

BTW could you add std::atomic to the list of formattable types in https://github.com/fmtlib/fmt/blob/master/doc/api.rst#standard-library-types-formatting?

template <typename FormatContext>
auto format(const std::atomic<T>& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<T, Char>::format(v.load(), ctx);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe its better to use load(std::memory_order_acquire) or even load(std::memory_order_relaxed)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants