{"id":8071,"date":"2023-08-21T07:37:20","date_gmt":"2023-08-21T07:37:20","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=8071"},"modified":"2024-05-05T09:17:11","modified_gmt":"2024-05-05T09:17:11","slug":"c23-a-modularized-standard-library-stdprint-and-stdprintln","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c23-a-modularized-standard-library-stdprint-and-stdprintln\/","title":{"rendered":"C++23: A Modularized Standard Library, std::print and std::println"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The C++23 standard library has very impressive improvements. In this post, I will write about the modularized standard library and the two convenience functions<code> std::print<\/code> and <code>std::println<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"579\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23-1030x579.png\" alt=\"\" class=\"wp-image-8087\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23-1030x579.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23-300x169.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23-768x432.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23-705x397.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/Cpp23.png 1280w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_new_%E2%80%9CHello_World%E2%80%9D\"><\/span>The new &#8220;Hello World&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each programming challenge in a new language starts with the &#8220;Hello World&#8221; program. Since C++98, this was our starting point: <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Hello World<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>; \n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Honestly, you have to skip your old habits in C++23. Here is the current &#8220;Hello World program:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">import std;\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>println(<span style=\"color: #CC3300\">&quot;Hello World&quot;<\/span>); \n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Let me analyze the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Modularized_Standard_Library\"><\/span>Modularized Standard Library<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++23 supports a modularized standard library. You say <code>import std;<\/code>, and you get the entire standard library. If you also want the global C functions such as <code>printf<\/code>, you must use <code>import std.compat;<\/code>. Here is the corresponding &#8220;Hello World&#8221; program using <code>printf<\/code>.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">import std.compat;\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    printf(<span style=\"color: #CC3300\">&quot;Hello World<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>); \n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The modularized standard library has two significant improvements: a significantly improved compilation time and usability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Significantly_improved_Compilation_Time\"><\/span>Significantly improved Compilation Time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Importing the standard library (import std) is literally for free. This means that the compilation times will drop significantly. The first experience numbers state that the complication times became faster by at least a factor of 10. The reason for this improvement is evident. Instead of successively expanding your header files, you import a module. For that reason, there is only one module you have to import only once. So far, only the MSVC compiler supports this C++23 feature: <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/cpp\/tutorial-import-stl-named-module?view=msvc-170\" data-type=\"link\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/cpp\/tutorial-import-stl-named-module?view=msvc-170\">Tutorial: Import the C++ standard library using modules from the command line<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Usability\"><\/span>Usability<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Assume you want to use the function <code>std::accumulate<\/code>. Do you know which header you have to include? Is it <code>&lt;numeric&gt;<\/code>, <code>&lt;functional&gt;<\/code>, or <code>&lt;algorithm&gt;<\/code>. Maybe, this was too easy for you. So what about <code>std::forward<\/code>, or why does the following program fail to compile? <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> list <span style=\"color: #555555\">=<\/span> {<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>, <span style=\"color: #FF6600\">4<\/span>};\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The {1, 2, 3, 4} is a <code>std::initializer_list&lt;int&gt;. <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use it, you have to include the header <code>&lt;initializer_list&gt;<\/code>. Of course, this header is automatically included when you use a container like <code>std::vector<\/code>. <code> <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Compare this with an <code>import std;<\/code> or <code>import std::compat; <\/code>and you are done. I know from my experience. Not only beginners often fail to use the correct header.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I didn&#8217;t know if you recognized it, but my C++23-ish &#8220;Hello World&#8221; program used a second feature of C++23:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"std_print_and_std_println\"><\/span><code>std::print<\/code> and <code>std::println<\/code><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The C++23 supports for both functions two overloads:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>std<code>::print<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span> class... Args <span style=\"color: #555555\">&gt;<\/span>\n  <span style=\"color: #007788; font-weight: bold\">void<\/span> print( std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">FILE<\/span><span style=\"color: #555555\">*<\/span> stream,\n              std<span style=\"color: #555555\">::<\/span>format_string<span style=\"color: #555555\">&lt;<\/span>Args...<span style=\"color: #555555\">&gt;<\/span> fmt, Args<span style=\"color: #555555\">&amp;&amp;<\/span>... args );\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span> class... Args <span style=\"color: #555555\">&gt;<\/span>\n  <span style=\"color: #007788; font-weight: bold\">void<\/span> print( std<span style=\"color: #555555\">::<\/span>format_string<span style=\"color: #555555\">&lt;<\/span>Args...<span style=\"color: #555555\">&gt;<\/span> fmt, Args<span style=\"color: #555555\">&amp;&amp;<\/span>... args );\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>std::println<\/strong><\/code><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span> class... Args <span style=\"color: #555555\">&gt;<\/span>\n  <span style=\"color: #007788; font-weight: bold\">void<\/span> println( std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">FILE<\/span><span style=\"color: #555555\">*<\/span> stream,\n              std<span style=\"color: #555555\">::<\/span>format_string<span style=\"color: #555555\">&lt;<\/span>Args...<span style=\"color: #555555\">&gt;<\/span> fmt, Args<span style=\"color: #555555\">&amp;&amp;<\/span>... args );\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span> class... Args <span style=\"color: #555555\">&gt;<\/span>\n  <span style=\"color: #007788; font-weight: bold\">void<\/span> println( std<span style=\"color: #555555\">::<\/span>format_string<span style=\"color: #555555\">&lt;<\/span>Args...<span style=\"color: #555555\">&gt;<\/span> fmt, Args<span style=\"color: #555555\">&amp;&amp;<\/span>... args );\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The first difference between <code>std::print <\/code>and <code>std::println<\/code> is obvious: <code>std::prinln<\/code> adds a line break. These are the more interesting points:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Variadic_Template\"><\/span>Variadic Template<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::prin<\/code>t and <code>std::println<\/code> are variadic templates. Variadic templates are templates that can accept an arbitrary number of arguments. Its arguments are perfectly forwarded. <code>std::print<\/code> and <code>std::println<\/code> are the type-safe variant of <code>printf<\/code>. With <code>printf <\/code>you must specify the format string; with <code>std::print<\/code> and <code>std::println<\/code> you use placeholders in the format string. In general, the compiler deduces the type for the placeholders by applying the rules of<code> std::format<\/code> in C++20. Consequentially,<code> std::print<\/code> and <code>std::println<\/code> in C++23 seem to be syntactic sugar for <code>std::format<\/code> in C++20. Here is the modified C++23-ish &#8220;Hello World&#8221; program using <code>std::format<\/code>. <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">import std;\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ std::println(&quot;Hello World&quot;); <\/span>\n     std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, <span style=\"color: #CC3300\">&quot;Hello World&quot;<\/span>);\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to know more about variadic templates, perfect forwarding, and<code> std::format<\/code>, read my previous posts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/variadic-templates\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/tag\/variadic-templates\/\">Variadic Templates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\/\">Perfect Forwarding<\/a><\/li>\n\n\n\n<li><code><a href=\"std::format\">std::format<\/a><\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Unicode_Support\"><\/span>Unicode Support<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I wrote that <code>std::print<\/code> and <code>std::println<\/code> in C++23 seem to be syntactic sugar for <code>std::format<\/code> in C++20. This is not true because<code> std::print<\/code> and <code>std::println<\/code> support Unicode. Let me quote parts from the proposal <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2022\/p2093r14.html\" data-type=\"link\" data-id=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2022\/p2093r14.html\">P2093R14<\/a>: <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Another problem is formatting of Unicode text: <\/em><\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;\u041f\u0440\u0438\u0432\u0435\u0442, \u03ba\u03cc\u03c3\u03bc\u03bf\u03c2!&quot;<\/span>; \n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>If the source and execution encoding is UTF-8 this will produce the expected output on most GNU\/Linux and macOS systems. Unfortunately on Windows it is almost guaranteed to produce <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mojibake\">mojibake<\/a> despite the fact that the system is fully capable of printing Unicode, for example<\/em><\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">\u2568\u0192\u2564\u00c7\u2568\u2555\u2568\u2593\u2568\u2561\u2564\u00e9 \u256c\u2551\u2567\u00ee\u2567\u00e2\u256c\u255d\u256c\u2510\u2567\u00e9!\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>even when compiled with <code>\/utf-8<\/code> using Visual C++ (<a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2022\/p2093r14.html#biblio-msvc-utf8\">[MSVC-UTF8]<\/a>). This happens because the terminal assumes code page 437 in this case independently of the execution encoding.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>With the proposed paper <\/em><\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #006699; font-weight: bold\">std:<\/span><span style=\"color: #555555\">:<\/span>print(<span style=\"color: #CC3300\">&quot;\u041f\u0440\u0438\u0432\u0435\u0442, \u03ba\u03cc\u03c3\u03bc\u03bf\u03c2!&quot;<\/span>);\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>will print <code>\"\u041f\u0440\u0438\u0432\u0435\u0442, \u03ba\u03cc\u03c3\u03bc\u03bf\u03c2!\"<\/code> as expected allowing programmers to write Unicode text portably using standard facilities.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Arbitrary_Output_Stream\"><\/span>Arbitrary Output Stream<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both variants <code>std::print<\/code> and <code>std:println<\/code> have an overload that accepts an arbitrary output stream. By default, the output stream is <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/c\/std_streams\" data-type=\"link\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/c\/std_streams\">stdout<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is more to<code> std::format<\/code> and, consequentially, <code>std::print<\/code> and <code>std::println <\/code>in C++23. In C++23, you can format a container of the Standard Template Library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Formatted_Output_of_a_Container\"><\/span>Formatted Output of a Container<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following program shows how you can directly display a container of the STL. So far, no C++ compiler supports this feature. Only the brand-new Clang compiler with the<code> libc++<\/code> instead of the <code>libstdc++<\/code> enables me to use this feature partially. In a fully conforming C++23 implementation,  I could use<code> std::println<\/code> instead of<code> std::format<\/code>. <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ formatVector.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;format&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;vector&gt;<\/span>\n    \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n  std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> myInts{<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">2<\/span>, <span style=\"color: #FF6600\">3<\/span>, <span style=\"color: #FF6600\">4<\/span>, <span style=\"color: #FF6600\">5<\/span>, <span style=\"color: #FF6600\">6<\/span>, <span style=\"color: #FF6600\">7<\/span>, <span style=\"color: #FF6600\">8<\/span>, <span style=\"color: #FF6600\">9<\/span>, <span style=\"color: #FF6600\">10<\/span>};\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myInts);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{::+}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myInts);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{::02x}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myInts);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{::b}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myInts);\n\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n  std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>string<span style=\"color: #555555\">&gt;<\/span> myStrings{<span style=\"color: #CC3300\">&quot;Only&quot;<\/span>, <span style=\"color: #CC3300\">&quot;for&quot;<\/span>, <span style=\"color: #CC3300\">&quot;testing&quot;<\/span>, <span style=\"color: #CC3300\">&quot;purpose&quot;<\/span>};\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myStrings);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{::.3}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, myStrings);\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">I use in this example a std::vector&lt;int&gt; and a std::vector&lt;std::string&gt;.  Using {:} as a placeholder displays both containers (lines 1 and 2) directly. Using two colons <code>{::}<\/code> inside the placeholder allows you to format the elements of the <code>std::vector.<\/code> The format specifier follows the second colon. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::vector&lt;int&gt;:<\/code> The vector&#8217;s elements have a + sign {::+}, are hexadecimal aligned to 2 characters with the 0 as the fill character <code>{::02x}<\/code>, and are binary displayed<code> {::b}<\/code>. <\/li>\n\n\n\n<li><code>std::vector&lt;std::string&gt;<\/code>:  Each string is truncated to its first 3 characters:<code> {::.3}<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following screenshot shows the output of the program on the <a href=\"https:\/\/godbolt.org\/z\/xhsaoPK83\" data-type=\"link\" data-id=\"https:\/\/godbolt.org\/z\/xhsaoPK83\">Compiler Explorer<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"160\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/formatVector.png\" alt=\"\" class=\"wp-image-8095\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/formatVector.png 415w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/08\/formatVector-300x116.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Whats_Next\"><\/span>What&#8217;s Next? <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In my next post about the standard library improvements in C++23, I will present the extended interface of <code>std::optional<\/code> and the new data type <code>std::expected<\/code> for error handling. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++23 standard library has very impressive improvements. In this post, I will write about the modularized standard library and the two convenience functions std::print and std::println. The new &#8220;Hello World&#8221; Each programming challenge in a new language starts with the &#8220;Hello World&#8221; program. Since C++98, this was our starting point: #include &lt;iostream&gt; int main() [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":8087,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[378],"tags":[454,443],"class_list":["post-8071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-23","tag-format","tag-modules"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=8071"}],"version-history":[{"count":37,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8071\/revisions"}],"predecessor-version":[{"id":9527,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8071\/revisions\/9527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8087"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=8071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=8071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=8071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}