Skip to content

Add formatters for container adapters - #3279

Merged
vitaut merged 6 commits into
fmtlib:masterfrom
ShawnZhong:container-adapter
Feb 9, 2023
Merged

vitaut merged 6 commits into
fmtlib:masterfrom
ShawnZhong:container-adapter

Conversation

@ShawnZhong

Copy link
Copy Markdown
Contributor

Close #3215.

https://eel.is/c++draft/container.adaptors.format specifies formatting for container adapters (std::stack, std::queue, and std::priority_queue in particular).

Not sure if the implementation should go into ranges.h or std.h. I'm putting it in ranges.h for now because I don't want to make std.h depend on ranges.h.

Container adapters have a protected member Container c, which can be exposed by defining a derived class.

Comment thread include/fmt/ranges.h Outdated
struct formatter<T, Char,
enable_if_t<detail::is_container_adaptor_like<T>::value>> {
private:
struct formatter<typename T::container_type, Char> container_formatter;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

struct not needed here and I suggest using inheritance instead of composition to avoid redefining parse.

Also please apply clang-format, indent is a bit off.

@vitaut
vitaut merged commit 581c629 into fmtlib:master Feb 9, 2023
@vitaut

vitaut commented Feb 9, 2023

Copy link
Copy Markdown
Contributor

Thank you!

Comment thread test/ranges-test.cc
Comment on lines +450 to +452
// Note: The output is formatted as a string because the underlying
// container is a string. This behavior is conforming to the standard
// [container.adaptors.format].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is inconsistent with formatting containers and looks like an oversight. I think we should open an LWG issue.

@ShawnZhong
ShawnZhong deleted the container-adapter branch February 9, 2023 02:34
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.

Add formatters for STL container adapters

2 participants