Formatting C++ strings without std::format: vformat and stringstream
Like printf, std::format requires the format string to be a string literal; C++ therefore also offers std::vformat[1], which takes the format as a std::string_view[2] and so allows a variable. Since std::vformat does not support variadic arguments directly, the arguments are bundled by std::make_format_args into std::format_args. Before C++20, strings were assembled with std::stringstream[3] and std::wstringstream by inserting data with operator << and reading the result through str(); these classes were not removed, and the example compiles both under C++20 and under C++14. Formatting with these streams is set by inserting manipulators[4] such as std::fixed, std::setprecision, std::hex, std::dec, std::setw and std::setfill from <iomanip>, and the settings apply to all subsequent data inserted into the stream.
Sources
-
JPZenn JA
【C++言語入門STL編】 第4回 文字列のフォーマット(他の方法)






