Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One thing you can do is if you do not otherwise need the <code>vformat()</code> function, move its implementation into the <code>format()</code> function (untested):</p> <pre><code>#include &lt;stdarg.h&gt; #include &lt;string.h&gt; #include &lt;assert.h&gt; #include &lt;string&gt; #include &lt;vector&gt; std::string format(const char *fmt, ...) { va_list ap; enum {size = 1024}; // if you want a buffer on the stack for the 99% of the time case // for efficiency or whatever), I suggest something like // STLSoft's auto_buffer&lt;&gt; template. // // http://www.synesis.com.au/software/stlsoft/doc-1.9/classstlsoft_1_1auto__buffer.html // std::vector&lt;char&gt; buf( size); // // where you get a proper vsnprintf() for MSVC is another problem // maybe look at http://www.jhweiss.de/software/snprintf.html // // note that vsnprintf() might use the passed ap with the // va_arg() macro. This would invalidate ap here, so we // we va_end() it here, and have to redo the va_start() // if we want to use it again. From the C standard: // // The object ap may be passed as an argument to // another function; if that function invokes the // va_arg macro with parameter ap, the value of ap // in the calling function is indeterminate and // shall be passed to the va_end macro prior to // any further reference to ap. // // Thanks to Rob Kennedy for pointing that out. // va_start (ap, fmt); int needed = vsnprintf (&amp;buf[0], buf.size(), fmt, ap); va_end( ap); if (needed &gt;= size) { // vsnprintf reported that it wanted to write more characters // than we allotted. So do a malloc of the right size and try again. // This doesn't happen very often if we chose our initial size // well. buf.resize( needed + 1); va_start (ap, fmt); needed = vsnprintf (&amp;buf[0], buf.size(), fmt, ap); va_end( ap); assert( needed &lt; buf.size()); } return std::string( &amp;buf[0]); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload