Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I take ownership of a C++ std::string char data without copying and keeping std::string object?
    primarykey
    data
    text
    <p>How can I take ownership of std::string char data without copying and withoug keeping source std::string object? (I want to use moving semantics but between different types.)</p> <p>I use the C++11 <a href="http://en.wikipedia.org/wiki/Clang" rel="nofollow">Clang</a> compiler and <a href="http://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29" rel="nofollow">Boost</a>.</p> <p>Basically I want to do something equivalent to this:</p> <pre><code>{ std::string s(“Possibly very long user string”); const char* mine = s.c_str(); // 'mine' will be passed along, pass(mine); //Made-up call s.release_data(); // 's' should not release data, but it should properly destroy itself otherwise. } </code></pre> <p>To clarify, I do need to get rid of std::string: further down the road. The code deals with both string and binary data and should handle it in the same format. And I do want the data from std::string, because that comes from another code layer that works with std::string.</p> <p>To give more perspective where I run into wanting to do so: for example I have an asynchronous socket wrapper that should be able to take both std::string and binary data from user for writing. Both "API" write versions (taking std::string or row binary data) internally resolve to the same (binary) write. I need to avoid any copying as the string may be long.</p> <pre><code>WriteId write( std::unique_ptr&lt; std::string &gt; strToWrite ) { // Convert std::string data to contiguous byte storage // that will be further passed along to other // functions (also with the moving semantics). // strToWrite.c_str() would be a solution to my problem // if I could tell strToWrite to simply give up its // ownership. Is there a way? unique_ptr&lt;std::vector&lt;char&gt; &gt; dataToWrite= ?? // scheduleWrite( dataToWrite ); } void scheduledWrite( std::unique_ptr&lt; std::vecor&lt;char&gt; &gt; data) { … } </code></pre> <p>std::unique_ptr in this example to illustrate ownership transfer: any other approach with the same semantics is fine to me.</p> <p>I am wondering about solutions to this specific case (with std::string char buffer) and this sort of problem with strings, streams and similar general: tips to approach moving buffers around between string, stream, std containers and buffer types.</p> <p>I would also appreciated tips and links with C++ design approaches and specific techniques when it comes to passing buffer data around between different API's/types without copying. I mention but not using streams because I'm shaky on that subject.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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