Stdio or iostreams? – A Modest Alternative for C++
&gt; print(&quot;hello&quot;,who,&quot;answer&quot;,answer,&#x27;\n&#x27;); -- This is cute, ... but what if you need an integer in hex, or a floating-point number to a particular precision, and so forth?<p>Then you can use a helper function to convert them to string objects first.<p><pre><code>    print(&quot;A:&quot;, hex(r.a, 4), &quot; X:&quot;, hex(r.x, 4), &quot; C:&quot;, integer(c, -10, &#x27; &#x27;), &quot;\n&quot;);
</code></pre>
If your string class is smart enough to do small-string optimization (SSO), then this won&#x27;t even need heap allocation, so the overhead is really very minimal.<p>This is basically what I do for my string library. But I also have a special format mode for things like UI localization:<p><pre><code>    print(tr(&quot;{1} is the value of {0}\n&quot;), format{name, value});
    &#x2F;&#x2F;tr returns &quot;{0}の値は{1}です\n&quot; for Japanese locale
</code></pre>
This goes further than printf by allowing you to very easily reorder the arguments. (omitting the indexes and using just {} makes them sequential.) [I&#x27;ve heard printf can do this too, but I don&#x27;t know if that&#x27;s standardized. I&#x27;ve never seen anyone do it in the wild.]<p>The format{} builds a list of values [I convert them to strings but you could use an any type if you wanted), and when print() gets a const format&amp; as a type, it applies the list to the current string before it. It would be a trivial exercise to extend the token syntax to support formatting rules, eg &quot;{0,-4h}&quot; or what not.<p>Further, adapters can be added outside the library. So for SQL, one can have bindings via:<p><pre><code>    database.execute(&quot;select * from user where name=? and age &gt;= ?&quot;,
      escape{name, minimumAge});
</code></pre>
Still, I&#x27;ll freely confess that this isn&#x27;t in the spirit of the C++ standard library: you pay a tiny bit of performance overhead in return for niceness. But unless your application is I&#x2F;O bound to string formatting, I strongly believe it&#x27;s worth the (minimal) cost.<p>String interpolation would make C++ print statements even nicer, but that&#x27;s unlikely to ever arrive.