Is it possible to take memory from std :: string (e.g. string move ctor)?

If I have my inner class, which is my own version of vector<char> (I control the source), and for example I cannot change it as std::string , is there any way to steal memory from std::string , like std::string move constructor.

So something like this:

 std::string str{"abcdefghijklmnopqrstu"}; MyVectorCharClass mvc(std::move(str)); // Constructor takes memory from str 

I think I heard about some future suggestions to add .release() to std::string or std::vector , but I'm talking about the present.

+5
source share
1 answer

No. The buffer that controls std::string is closed to it. You can access it via &str[0] , but string will still own it and destroy it when it goes out of scope. You have no way to tell str that it now owns another buffer or sets its base buffer to nullptr or in any other way make it not delete that buffer.

This is a std::string job. He owns his buffer and is not going to give it up.

+6
source

All Articles