Live data from Hacker News

Formatting text in C++: Old and new ways

mariusbancila.ro

71–73 of 73 posts

Re: Formatting text in C++: Old and new ways

#71

Earlier quoted context omitted.

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 th…

Rust does not use reflection for this. Reflection support in rust isn’t actually much better than in C++.

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++: Old and new ways

#72

Earlier quoted context omitted.

Nothing but (1) notation wise not very different from the old printf which is looked down upon. (2) f"{name}'s hobby is {hobby} " would read like a novel and there a lot less comma seperated arguments. (3) std::format is quite a lot of characters to type for something so ubiquitous.

(2) is a point I firmly agree with (though not everyone does), but it’s a hard one. Here’s the way I think about it. I don’t think I’m wrong but I’m absolutely open to being told otherwise. C++ is a language. It has a standard library. The library depends on the language, but the language shouldn’t depend on the library. This is because many applications cannot use the standard library, or parts of it. The conceptual…

I understand what you are saying.

I wonder/doubt if the comma would have to be an explicit step as part of the hand over to the std lib. The comma is a separator that the programmer would use, but does the compiler need that? The compiler needs to translate 1 argument to multiple arguments of multiple types, such that stdlib can receive all info. (The original fstring indeed cannot be expanded by the lib itself, since that would give stdlib a special status). So the needed language feature is a one to multi args translation, where at compile time all types are known. That would mean that in the context of an assignment (in stead of a function argument), the f string does not make sense. In that case the compilation can simply fail, no?

I guess the handing over the multiple args of different types is a problem. Not all infinite combinations can be solved by templating and I guess even if it could, the header would contain too much logic since the template expansion needs to happen from the header?

Re: Formatting text in C++: Old and new ways

#73
post #52
post #49

Earlier quoted context omitted.

>but generally C++ programmers prefer to be explicit and include the namespaces. why, though? Collisions in the stdlib? stdlib is too new to not be the default for these names?

The using declaration modifies your namespace. From the docs: namespace X { using ::f; // global f is now visible as ::X::f using A::g; // A::g is now visible as ::X::g } void h() { X::f(); // calls ::f X::g(); // calls A::g } Link to docs: https://en.cppreference.com/w/cpp/language/namespace#Using-d...

Thanks
Post reply on HN