cpp
cpp copied to clipboard
Strings exercise wrongly states that sprintf is preferred for string formatting
In C++, string formatting using sprintf
is absolutely not the preferred way to do it, and is incompatible with std::string
to boot. This should be corrected to recommend std::stringstream
instead.
Which exercise are talking about?
https://github.com/exercism/cpp/blob/main/exercises/concept/strings/.docs/after.md
I see. I think this is a first attempt at creating concepts and concept exercises.
I agree that std::ostringstream
should be preferred in general.
With C++ 20 we have the introduction of std::format which is more efficient and, in my opinion, more familiar and easier to use. As sprintf it also works with a formatting string and a variable set of arguments that are replaced in the formatting string.
std::format
support is brand new as of GCC 13 and Clang 15 (Clang 14 does not support it unless built with experimental features enabled). I still use {fmt}
(and probably will for the foreseeable future), but I do think std::format
is the correct solution here. It just may ostracize some users who do not have new enough STLs.
Currently the C++ track uses C++17 because C++20 might not be available on some LTS Linux releases.
When we get to a point where we switch to C++20 and std::format
is supported by the C++ compiler on those LTS releases I agree, we should recommend std::format
. (Personally whenever I have the choice I'm happy with {fmt}.)
@willeccles The string concept has been updated and can be seen on the main branch. It is not yet available on the website, but you can browse the files here on github to see if they have improved.
If you think there is something that should be added or changed, please open a new issue.