Possible Duplicate:
initializing std::string from char* without copy
I'd really like to figure out how to pass "ownership" of a heap allocated char* to a std::string instance without any additional malloc's or memcpy's. Does anybody know how to build a string in such a manner. Ideally it would be something like:
const char* buffer = someCMethodThatReturnsAString();
try {
string strBuffer(PlacementAllocator(buffer));
} catch (...) {
free(buffer);
}
but without actually copying the buffer. Instead I want the buffer to become the property of the string and to be free() when the string object is deleted (i.e. goes out of scope).
Is this something that the allocator in the standard library can support or would I need to build an allocator myself?