Motivation:
I'm using some library (which I don't control) with the following function:
char* foo(/*some parameters here */);
Which returns a very long, null-terminated string. Now, I want to use the function
void bar(const std::string& s);
which is available in another library I'm using (that is, I don't control the signatures of either of these functions). I want to pass the result of foo()
to bar()
- without copying it.
Question:
I want to wrap a contiguous buffer of characters with a string object, so it can be passed as a const std::string&
. How can I do this?
Notes:
std::string
is not an option, as it doesn't have the appropriate virtual methods. That basically leaves us with assuming things about the implementation (or writing different code for different implementations), and 'hand-constructing' an std::string
instance. Thus, the question seems to be about doing that.No, you can't. Review your requirements and eliminate the need for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With