C++ Logo


Advanced search

[std-proposals] Attaching and detaching memory from std::vector or std::string

From: ஜெய்கணேஷ் குமரன் <jaiganesh.kumaran_at_[hidden]>
Date: Fri, 22 Dec 2023 16:15:12 +0000
Hello all,

At the moment there appeärs to be no way to create a std::vector or std::string from an existing memory buffer, or take ownership of the memory owned by std::vector or std::string.
This is very limiting in interoperability scenarios where you may not be able to expose a std::vector or std::string directly.

Technically, a custom allocator could be used that provides existing memory but still the actual objects cannot are not considered.

I propose having .attach and .detach member functions on std::vector and std::string.
They should take in or return the following respectively, in a structure perhaps: pointer to the data (.data), count of the elements (.size/length), capacity of the buffer (.capacity) and the allocator instance.

Note: I do not have the time to participate in standardisation in order to open formal proposals, but I wish someöne else does based on my ideas.

Jaiganésh Kumaran.

Received on 2023-12-22 16:15:18