3 ms·
> a feature C manages to have, but here's Rust's This is clearly disingenuous. C's string formatting is a completely different thing from that Rust snippet or
by kllrnohj 5y ago
> a feature C manages to have, but here's Rust's
This is clearly disingenuous. C's string formatting is a completely different thing from that Rust snippet or your snide remark about std::format.
I'd rather use printf than cout, I think iostream is clunky, but printf is definitely a footgun & a fairly common source of bugs itself. So much so that compilers had to add support for specifically recognizing printf() calls & validating the inputs.
Also don't forget C still doesn't have standard placeholders for sized types. Gotta use that derpy PRId64 & friends which makes printf() almost as ugly looking as iostreams.
- tialaramex 5y agoThe C standard could insist they perform print format validation and thus make it safe for string literals. As you note, popular compilers do this today so it would merely be requiring what is in fact common practice. As published C++ 20 allows a compiler to swallow this nonsense: if (untested_condition) result = std::format("Oops {} {} {} {}", only, three, parameters); ... and spit out a program that will only blow up when untested_condition becomes true in production. Fortunately sanity prevailed as I understand it, and the revised document correctly requires that this is a compile time error.
- kllrnohj 5y agoThat's again a disingenuous phrasing. The compiler didn't swallow any nonsense. std::format is just a function, there's no direct compiler support for this. The {fmt} library did validation at runtime (which still needs to happen if the string isn't a constant!). The C++ working group then pushed to make it a compile time error in the cases it could be as part of promoting {fmt} into the standard library, which {fmt} did on existing C++20 compilers & that improvement has been folded into the spec.
- tialaramex 5y agoprintf() is likewise just a function. If we're not calling out std::format("{}{}",oops); as nonsense then we'd have no right to complain about printf("%s%d",oops) either. I say it is nonsense because it isn't sense.
- kllrnohj 5y agoOf course we can complain. First, std::format is already improved, so this is moot anyway. But more importantly, std::format always caught the error & threw a well defined exception. printf() does no such thing. It has no validation that your inputs matched the format spec. std::format always had that. So does std::cout & iostreams, for that matter.