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

Analyze universal-formatter example #6

Open
FranciscoThiesen opened this issue May 16, 2024 · 4 comments
Open

Analyze universal-formatter example #6

FranciscoThiesen opened this issue May 16, 2024 · 4 comments
Assignees

Comments

@FranciscoThiesen
Copy link
Member

@lemire found this issue here: bloomberg/clang-p2996#37

That mentions the universal_formatter (which does something similar to the serialization we are currently able to do).

Some aspects that I want to research:

  • Are there any useful patterns for us to adopt to simplify our codebase?
  • What would be the ergonomic impact to change to the universal_formatter approach (i.e need to declare the formatter for every struct used in the process (bad) ).
@FranciscoThiesen FranciscoThiesen self-assigned this May 16, 2024
@lemire
Copy link
Member

lemire commented May 16, 2024

One of the benefit of looking at this example is that it follows closely what people seek to do with the standard.

@Yaraslaut
Copy link
Contributor

Yaraslaut commented May 17, 2024

Hi,
I've been playing around with reflections to serialize into different formats and I think I can add something to this point:

What would be the ergonomic impact to change to the universal_formatter approach (i.e need to declare the formatter for every struct used in the process (bad) ).

See https://godbolt.org/z/nG7ad4f9T
Basically you can make compiler instantiate formatter as long as you can encapsulate it inside function

@FranciscoThiesen
Copy link
Member Author

@Yaraslaut pretty cool example and indeed you don't need to explicitly instantiate the formatter. Do you think/know if there is any benefit out using this formatter approach versus what we are currently doing?

@Yaraslaut
Copy link
Contributor

I looked at fast_to_json_string and think that the only difference is the lack of handling of base classes.
Also, reflections allow you to make formatting a bit more generic so you can serialize into different formats (https://github.com/Yaraslaut/form?tab=readme-ov-file#serialization-into-different-formats)

As for universal_formatter you can add something like

template<typename T>
struct std::formatter<T> : universal_formatter{};

and then you can print any structure with std::print("{}",S{})

@lemire lemire mentioned this issue Jul 16, 2024
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

No branches or pull requests

3 participants