title: writing directly to std::string internal buffers
body: <p>I was looking for a way to stuff some data into a string across a DLL boundary.  Because we use different compilers, all our dll interfaces are simple char*.</p>
  
  <p>Is there a correct way to pass a pointer into the dll function such that it is able to fill the string buffer directly?</p>
  
  <pre><code>string stringToFillIn(100, '\0');
  FunctionInDLL( stringToFillIn.c_str(), stringToFillIn.size() );   // definitely WRONG!
  FunctionInDLL( const_cast&lt;char*&gt;(stringToFillIn.data()), stringToFillIn.size() );    // WRONG?
  FunctionInDLL( &amp;stringToFillIn[0], stringToFillIn.size() );       // WRONG?
  stringToFillIn.resize( strlen( stringToFillIn.c_str() ) );
  </code></pre>
  
  <p>The one that looks most promising is &amp;stringToFillIn[0] but is that a correct way to do this, given that you'd think that string::data() == &amp;string[0]?  It seems inconsistent.</p>
  
  <p>Or is it better to swallow an extra allocation and avoid the question:</p>
  
  <pre><code>vector&lt;char&gt; vectorToFillIn(100);
  FunctionInDLL( &amp;vectorToFillIn[0], vectorToFillIn.size() );
  string dllGaveUs( &amp;vectorToFillIn[0] );
  </code></pre>