Live data from Hacker News

Formatting text in C++: Old and new ways

mariusbancila.ro

31–40 of 73 posts

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

#31

Earlier quoted context omitted.

That's why some newer languages have 'const are the default'.

I'm pretty sure it's the reference that makes the data not be copied.

Copying the data to a const makes little sense in this case. The extra & choice that has emerged makes things more complicated than needed. The sad faith of this old language.

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

#32

Something else to consider is compile time versus runtime validation with formatting libraries, e.g. due to passing the wrong number or type of arguments. The Abseil str_format library does compile time validation for both when possible: https://abseil.io/docs/cpp/guides/format

{fmt} certainly does this too. It works quite nicely with the clangd language server flagging a line as an error until the format string and arguments match.

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

#33

Earlier quoted context omitted.

For list comprehension, we have (C++23): `std::ranges::to (items | std::views::filter(shouldInclude) | std::views::transform(f))` it’s not quite `[f(x) for x in items if shouldInclude(x)]` but it’s the same idea.

To be honest, if that's the notation, i will not be very eager to jump on cpp23. That said, I admire people who's minds stay open for c++ improvements and make that effort.

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.

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

#34
post #28

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.

Typically, you'd just `using namespace std` on top of you file. Afterwards calls to `format` have the exact same length as `printf`.

I don't understand. I genuinely thought that using using namespace std is considered bad practice because of possible arising conflicts. Also you still need to write the word format (though you could alias it to one character, with same namespace conflict possibilities). Am i pedantic?

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

#35
post #28

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.

Typically, you'd just `using namespace std` on top of you file. Afterwards calls to `format` have the exact same length as `printf`.

This is questionable advice. In header files 'using namespace' should never be used, in implementation files it opens up some weird edge cases. Instead, do

   using fmt = std::format
and then use fmt(...) as the function call.

... at least that the current advice AIUI.

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

#36
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?

Your CPU is effectively a virtual machine with stuff like branch prediction, speculative execution w/rollback, pipelining, implicit parallelism, etc. etc.

Of course, it isn't able to do quite as much as a VM running in software (because fixed buffers for everything, etc.), but even so...

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

#37
post #23

Earlier quoted context omitted.

what a language

just consider const reference to be the default for non-primitive types

My rule of thumb is to use const references when the sizeof the type is larger than the sizeof a pointer or reference.

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

#39
post #28

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.

Typically, you'd just `using namespace std` on top of you file. Afterwards calls to `format` have the exact same length as `printf`.

This is not widely considered a good practice, please don't "using namespace std".

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

#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.
Post reply on HN