Assume that I have a function that gets const string&
as its input for example:
void foo(const string& s);
And then I have an internal buffer const char* buffer;
which I know the size of it.
I think if I create string inline, still one copy would happen:
foo(string(buffer, n));
But there is no need to copy the buffer because all things are constant and I just need the functionality of string class not the buffer that it creates.
I must mention that I am not sure that copy happens or not, But looking at the constructor of string all of them said that copy would happen. I don't know compiler optimization can understand such situations or not and I could not find a way to sure that copy happened or not.
Is there any way to use an external buffer for string, or at least a way to sure that copy happens or not. I am using std string and c++11 currently.