Reputation: 81
My programmer has lots of memcpy. I want to avoid them.
I want to get a struct like boost::string_ref. I want to know.
uint32_t len = 20;
char *p = new char[len];
memset(p, 0x00, len)
memcpy(p, "aaaa", 4);
string str(p, 4);// whether is use memcpy or not ?
const string str2(p, 4); //whether is use memcpy or not
//if it used memcpy, how to avoid memcpy ?
string_ref->string
string_view->string
string->string_ref
string->string_view
char* ->string
string->string
please tell me how to judge whether function used memcpy ?
Upvotes: 1
Views: 345
Reputation: 51850
Use an std::string_view to avoid copying the string.
#include <string_view>
// ...
const string_view strview(p, 4);
Keep in mind that string views are not guaranteed to be null-terminated, so be careful when using them in APIs that expect a null-terminated char*
. Also, when the string they are viewing goes out of scope, the string view itself becomes invalid (just like a pointer would.)
Upvotes: 2