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 support for std::byte to fmt::join #1981

Closed
hellow554 opened this issue Nov 5, 2020 · 7 comments
Closed

Add support for std::byte to fmt::join #1981

hellow554 opened this issue Nov 5, 2020 · 7 comments

Comments

@hellow554
Copy link

There is the std::byte type since C++17.

It would be nice to have a built-in way to format them properly.

My current ad-hoc solution is

template<> struct fmt::formatter<std::byte> : fmt::formatter<int> {
    template<class FormatContext> auto format(const std::byte &b, FormatContext &ctx) {
        return fmt::formatter<int>::format(std::to_integer<int>(b), ctx);
    }
};
@vitaut
Copy link
Contributor

vitaut commented Nov 5, 2020

std::byte is already supported:

#include <fmt/core.h>

int main() {
  fmt::print("{}", std::byte(42));
}

prints 42 (https://godbolt.org/z/MKccMT).

@vitaut vitaut closed this as completed Nov 5, 2020
@hellow554
Copy link
Author

hellow554 commented Nov 5, 2020

My bad:

https://godbolt.org/z/f3oTno

#include <cstddef>
#include <vector>

#include <fmt/core.h>
#include <fmt/format.h>

int main() {
    std::vector<std::byte> vv;

    fmt::print("{}", fmt::join(vv, ","));
}

doesn't work, so this is an issue

@vitaut vitaut changed the title Add support for std::byte Add support for std::byte to fmt::join Nov 5, 2020
@vitaut vitaut reopened this Nov 5, 2020
@MikeGitb
Copy link

Could this be related to #2014?

Btw.: I'd find it much more reasonable, if std::byte got formatted as hex by default (ideally with leading zeros if necessary) - it is supposed to represent raw memory after all, not an integral number.

@hellow554
Copy link
Author

@MikeGitb it was an example to get it working for me. You can of course use {:x}, but having that as default? Hmm, not sure about it.

@MikeGitb
Copy link

@hellow554 : That was more meant for @vitaut, referring to the result of fmt::print("{}", std::byte(42)); not about your workaournd for container<std::byte>, which of course should be consistent with formatting of a single std::byte. but I guess thats off topic for the problem at hand.

kamibo added a commit to kamibo/fmt that referenced this issue Nov 25, 2020
Try to apply the same rules for iterator value type (join) as for
regular types.

Try to convert iterator value type to a formattable one.
For instance if the join operation refers to an enum class iterator
(such as std::byte) then try to use the enum class underlying type.

Also try to use a fallback formatter if available.
kamibo added a commit to kamibo/fmt that referenced this issue Nov 25, 2020
Try to apply the same rules for iterator value type (join) as for
regular types.

Try to convert iterator value type to a formattable one.
For instance if the join operation refers to an enum class iterator
(such as std::byte) then try to use the enum class underlying type.

Also try to use a fallback formatter if available.
kamibo added a commit to kamibo/fmt that referenced this issue Nov 25, 2020
Try to apply the same rules for iterator value type (join) as for
regular types.

Try to convert iterator value type to a formattable one.
For instance if the join operation refers to an enum class iterator
(such as std::byte) then try to use the enum class underlying type.

Also try to use a fallback formatter if available.
vitaut added a commit that referenced this issue Dec 3, 2020
@vitaut
Copy link
Contributor

vitaut commented Dec 3, 2020

Added a formatter specialization for std::byte in 4a6eadb, so it should work with join and other APIs. Thanks for reporting.

@vitaut vitaut closed this as completed Dec 3, 2020
@phprus
Copy link
Contributor

phprus commented Dec 3, 2020

Without a #include <cstddef>, there will be a compilation error in GCC-7:

include/fmt/format.h:3556:20: error: ‘byte’ is not a member of ‘std’
 FMT_FORMAT_AS(std::byte, unsigned);

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

No branches or pull requests

4 participants