{"id":10312,"date":"2025-01-20T11:55:06","date_gmt":"2025-01-20T11:55:06","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10312"},"modified":"2025-01-20T11:55:06","modified_gmt":"2025-01-20T11:55:06","slug":"stdformat-extension","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/stdformat-extension\/","title":{"rendered":"std::format Extension"},"content":{"rendered":"\n<p>Displaying the address of an arbitrary pointer in C++ 20 fails but succeeds with C++26.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"513\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions-1030x513.png\" alt=\"\" class=\"wp-image-10313\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions-1030x513.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions-300x149.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions-768x383.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions-705x351.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/FormarExtensions.png 1052w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">C++20<\/h3>\n\n\n\n<p>Only <code>void<\/code>, <code>const void<\/code>,  and <code>std::nullptr_t<\/code> pointer types are valid. If you want to display the address of an arbitrary pointer, you must cast it to<code> (const) void*.<\/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\">\/\/ formatPointer20.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\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n  <span style=\"color: #007788; font-weight: bold\">double<\/span> d <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123.456789<\/span>;\n\n  <span style=\"color: #0099FF; font-style: italic\">\/\/std::cout &lt;&lt; &quot;&amp;d&quot; &lt;&lt; std::format(&quot;{}&quot;, &amp;d) &lt;&lt; &#39;\\n&#39;;  <\/span>\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;static_cast&lt;void *&gt;(&amp;d): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #555555\">*&gt;<\/span>(<span style=\"color: #555555\">&amp;<\/span>d)) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;     \n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;static_cast&lt;const void *&gt;(&amp;d): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #555555\">*&gt;<\/span>(<span style=\"color: #555555\">&amp;<\/span>d)) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; \n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;nullptr: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, nullptr) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;           \n             \n}\n<\/pre><\/div>\n\n\n\n<p>In the <code>main<\/code> function, a <code>double<\/code> variable named <code>d<\/code> is initialized with the value <code>123.456789<\/code>. The program then prints the address of this variable using <code>std::format<\/code>.<\/p>\n\n\n\n<p>The first commented-out line attempts to print the address of<code> d<\/code> directly using <code>std::format(\"{}\", &amp;d)<\/code>. This line is commented out because it would result in a compilation error. The <code>std::format<\/code> function requires the pointer to be cast to <code>void*<\/code> or <code>const void*<\/code> to format it correctly.<\/p>\n\n\n\n<p>The next three lines demonstrate the correct way to format and print pointers using <code>std::format<\/code>. The first of these lines prints the address of<code> d<\/code> after casting it to <code>void*<\/code>. The second line prints the address of <code>d<\/code> after casting it to <code>const void*<\/code>. The third line prints the value <code>nullptr<\/code>, which represents a null pointer.<\/p>\n\n\n\n<p>Each of these lines uses <code>std::cout<\/code> to print a descriptive message followed by the formatted pointer. The <code>std::format<\/code> function is used to convert the pointer to a string representation that can be printed to the console.<\/p>\n\n\n\n<p>Here is the output of the program: <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"89\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20.png\" alt=\"\" class=\"wp-image-10318\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20.png 553w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20-300x48.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/figure>\n\n\n\n<p>Enabling the commented-out line causes a long error message:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"157\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-1030x157.png\" alt=\"\" class=\"wp-image-10319\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-1030x157.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-300x46.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-768x117.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-1536x234.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-1500x229.png 1500w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error-705x107.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/11\/formatPointer20Error.png 1804w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">C++26 <\/h3>\n\n\n\n<p>With C++26, you can directly output the pointer:<\/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\">\/\/ formatPointer26.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\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n  <span style=\"color: #007788; font-weight: bold\">double<\/span> d <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123.456789<\/span>;\n  <span style=\"color: #007788; font-weight: bold\">double<\/span><span style=\"color: #555555\">*<\/span> p <span style=\"color: #555555\">=<\/span> <span style=\"color: #555555\">&amp;<\/span>d;\n\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;&amp;d&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:P}&quot;<\/span>, p) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;  \n \n}\n<\/pre><\/div>\n\n\n\n<p>Honesty, the program does not compile. This contradicts <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/compiler_support\/26\" data-type=\"link\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/compiler_support\/26\">compiler support for C++26<\/a> and the proposal <a href=\"https:\/\/wg21.link\/P2510R3\">P2510R3<\/a>. Do you know why?<\/p>\n\n\n\n<p>A solution will be provided in the next article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>Concurrency will get with C++26 two nice features for lock-free data structures: Read-copy update and hazard pointers. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Displaying the address of an arbitrary pointer in C++ 20 fails but succeeds with C++26. C++20 Only void, const void, and std::nullptr_t pointer types are valid. If you want to display the address of an arbitrary pointer, you must cast it to (const) void*. \/\/ formatPointer20.cpp #include &lt;format&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main() { [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10313,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[454],"class_list":["post-10312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-format"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10312","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=10312"}],"version-history":[{"count":13,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10312\/revisions"}],"predecessor-version":[{"id":10568,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10312\/revisions\/10568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10313"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}