lundi 30 mars 2015

is there a way to set the length of a std::string without modifying the buffer content?

According to the statements made in the answers of these questions



.. in C++-11 it should be possible to call a C API function which takes a char pointer to store the output like this:



str::string str;
str.reserve(SOME_MAX_VALUE);
some_C_API_func(&str[0]);


But is there now a legal way to set the size of the string to the length of the (null terminated) content inside the buffer? S.th. like this:



std.set_size(strlen(&str[0]));


This is a very unesthetic abuse of std::string anyway I hear you say but I can't create a temporary char buffer on stack so I would have to create a buffer in heap and destroy it afterwards (which I want to avoid).


Is there a nice way to do this? Maybe not reserving but resizing and calling erase() afterwards would do it but it doesn't feel nice neighter..


Aucun commentaire:

Enregistrer un commentaire