title: Simpler way to create a C++ memorystream from (char*, size_t), without copying the data?
body: <p>I couldn't find anything ready-made, so I came up with:</p>
  
  <pre><code>class membuf : public basic_streambuf&lt;char&gt;
  {
  public:
    membuf(char* p, size_t n) {
      setg(p, p, p + n);
      setp(p, p + n);
    }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>char *mybuffer;
  size_t length;
  // ... allocate "mybuffer", put data into it, set "length"
  
  membuf mb(mybuffer, length);
  istream reader(&amp;mb);
  // use "reader"
  </code></pre>
  
  <p>I know of <code>stringstream</code>, but it doesn't seem to be able to work with binary data of given length.</p>
  
  <p>Am I inventing my own wheel here?</p>
  
  <p><strong>EDIT</strong></p>
  
  <ul>
  <li>It <strong>must not</strong> copy the input data, just create something that will iterate over the data.</li>
  <li>It must be portable - at least it should work both under gcc and MSVC.</li>
  </ul>