Timeline for Create a C++ string using printf-style formatting
Current License: CC BY-SA 3.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 3, 2019 at 7:17 | comment | added | alx - recommends codidact |
@TobySpeight That bug is amazing! They didn't do any checks on the code! Any tool would have detected that. Nowadays, I never use braces for one lines, and since I started doing that, I've never seen a bug like that. Even without GCC's help, tools like Linux's checkpatch.pl help finding those easily (tuxdiary.com/2015/03/22/check-kernel-code-checkpatch)
|
|
| Nov 22, 2018 at 14:35 | vote | accept | Toby Speight | ||
| Feb 12, 2018 at 10:00 | comment | added | Matthieu M. |
@TobySpeight: I regularly use if (...) { return x; } as a one-liner (literally). Just because there are braces does not necessarily require inserting new lines.
|
|
| Feb 12, 2018 at 9:08 | comment | added | Toby Speight |
On the nitpick - I think you're right, and I'll try to train myself to use braces. I have a much taller display than when I started writing C and C++, so can probably afford the space now! And I'm reminded that even when the statement is a single return, that still managed to cause a problem for Apple's HTTPS implementation. The mitigation in GCC, -Wmisleading-indentation is great, but it's better to not need it!
|
|
| Feb 10, 2018 at 18:08 | comment | added | Matthieu M. |
So, I'll agree that printf is not a panacea, but it has undeniable advantages of its own (raw speed, compile-time check of the format-string versus arguments) which make it very attractive in a number of circumstances. I have yet to see a credible alternative in C++ for string-based formatting (with compile-time formats), with constexpr I think it should be possible, and with better ergonomics, but I have yet to see any. As a parting thought, for asynchronous logging it is trivial to ship off printf arguments to offload the formatting to the logging thread, and that's really neat :)
|
|
| Feb 10, 2018 at 18:03 | comment | added | Matthieu M. |
@MartinYork I was talking about std::ostream only (and notably std::ostringstream which would be used here for formatting), std::cout/std::cin is a whole other can of worms indeed (find tips here). You'd expect a properly design stream library to outperform printf due to the lack of runtime parsing, the fact that it doesn't is a testimony of its irreducible performance overhead. On the other hand, format strings are nice, as they quickly give an overview of the result...
|
|
| Feb 10, 2018 at 17:51 | comment | added | Loki Astari | Having said all that I will caveat that printf is raw speed faster, but terrible! | |
| Feb 10, 2018 at 17:45 | comment | added | Loki Astari | The advantage C++ has is that it does not need to parse a format string at runtime, instead it can use the type information and generate the appropriate calls at compile time. Though the C++ streams does have extensive Local support not covered by C streams that reduces this advantage. | |
| Feb 10, 2018 at 17:45 | comment | added | Loki Astari |
performance: ostream has terrible formatting performance by desig does it? If you are using std::cout dont't forget to unbind C++ streams from C streams (that is costly), also untie std::cin from std::cout and dont manually flush. If you follow these three rules you will see the performance of C++ streams is comparable. Fail on any of the above and performance does crash.
|
|
| Feb 10, 2018 at 12:51 | history | answered | Matthieu M. | CC BY-SA 3.0 |