I have a source having a bunch of c-style strings hardcoded as const char, for each of those strings there's also a length integer in the code.
At it's simplest (it's a bit more complex in reality, but this works fine as example):
There's thousands of those strings, and they can be several hundreds or even thousand characters long.Code:const char hello[] = "hello"; int hello_length = 5; const char foo[] = "foo"; int foo_length = 5; const char bar[] = "bar"; int bar_length = 5;
I have functions that return one of the above...
if I change this to return a std::string instead of a const char*...Code:const char* getstring(const type& which_one_do_you_want, int& len) { // ... code to decide which one to use len = hello_length; return hello; }
Is there a way to prevent the string from doing an allocation+copy and instead make the string point to the const string ?Code:std::string getstring(const type& which_one_do_you_want, int& len) { // ... code to decide which one to use return std::string(hello, hello_length); }
possibly by "messing around" with the allocator template parameter to std::string ?
Or is there little point to returning a string in this case.




Reply With Quote