Live data from Hacker News

Formatting text in C++: Old and new ways

mariusbancila.ro

51–60 of 73 posts

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

#51
post #40

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.

[deleted]

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

#52
post #49

Earlier quoted context omitted.

Well you could write it as to (items | filter(shouldInclude) | transform(f)) if you really want to, but generally C++ programmers prefer to be explicit and include the namespaces.

>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...

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

#53
post #40

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.

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++.

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

#55
post #11

Why do execution times drop so drastically with increasing number of iterations? Shouldn’t the caches be filled after one iteration already? There is no JIT in C++, or is it?

> There is no JIT in C++, or is it?

This question doesn't make sense for the context*. C++ is Ahead of Time, by design; there is nothing to "just in time" compile.

JIT (as a concept) only makes sense if you are, in some way, abstracting your code from native machine code (usually via some sort of VM, like Python or Java's), which the "system" languages (C, Rust, Zig, C++, etc) do not.

What I think you are trying to reference are "runtime optimizations"; in which case, the answer is probably no. Base and STD C++ are pretty conservative about what they put into the runtime. Extended runtimes like Windows' and glibc might do some conditional optimizations, however.

* Yes, some contrarian is going to point out a project like Cling or C++/CLI. This is why I'm being very clear about "context".

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

#56
post #40

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.

> I am tired of writing my own print functions for random objects when debugging because the API developers did not bother to override the Won't you face the same problem in Rust? If the library developer did not derive the Debug trait, you're out of luck.

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

#58

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…

There's precedent for that in JavaScript's format strings. It also allows for cute ideas, like passing the template to a function that does SQL.

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

#60
post #11

Why do execution times drop so drastically with increasing number of iterations? Shouldn’t the caches be filled after one iteration already? There is no JIT in C++, or is it?

I only had a quick look at the code, but it looks like it's timing memory allocation. For example the sprintf part uses std::string str(100, '\0'). I'm not a C++ expert, but I believe this is essentially doing a malloc and memset of 100 bytes for every call to sprintf. So this is probably a poorly setup benchmark.
Post reply on HN