Re:Formatting Text in C++: The Old and the New Ways
by ruph123 on Saturday September 16, 2023 @07:37PM
(#37539929)
Will C++ ever get the possibility to just print the contents of an object like Rust does (with the automatic debug trait)?
I am tired of writing my own print functions for random objects when debugging because the API developers did not bother to override the <<operator. One of those things that are hard to accept when coming back from Rust.
[ Parent
| on HN ]
Re:Formatting Text in C++: The Old and the New Ways
by i-use-nixos-btw on Saturday September 16, 2023 @07:52PM
(#37540023)
Not until we get reflection. And reflection efforts seem to be at an impasse, so I don’t imagine we will see it for a few years at least.
I will add, though, that I’ve found copilot to be very handy when it comes to generating formatting. Last week I used it while writing fmt::formatter specialisations for a library with 50 structs, some having over 20 members. Writing all of them took about 10 minutes. I dare say the same would hold for operator<< overloads.
Re:Formatting Text in C++: The Old and the New Ways
by ComputerGuru on Sunday September 17, 2023 @01:43AM
(#37541880)
Rust does not use reflection for this. Reflection support in rust isn’t actually much better than in C++.
Re:Formatting Text in C++: The Old and the New Ways
by i-use-nixos-btw on Sunday September 17, 2023 @05:08PM
(#37549325)
I’m not a rust dev, but looking into how the Debug trait works, it’s basically what we’d need compile time reflection to get in C++.
It generates code that uses the class name and class members. In C++ we have no way of doing that (without hacky macro stuff).
Re:Formatting Text in C++: The Old and the New Ways
by vvanders on Saturday September 16, 2023 @09:36PM
(#37540611)
The fact that Rust also has a pretty-print("{:#}")[1] a single character away is also really convenient. When you're working with JSON or debug printing types it's nice to be able to format that without reaching for external tools.
Re:Formatting Text in C++: The Old and the New Ways
by shilangyu on Sunday September 17, 2023 @03:03AM
(#37542285)
> I am tired of writing my own print functions for random objects when debugging because the API developers did not bother to override the <<operator.
Won't you face the same problem in Rust? If the library developer did not derive the Debug trait, you're out of luck.