According to the statements made in the answers of these questions
- Is writing to &str[0] buffer (of a std:string) well-defined behaviour in C++11?
- Is it legal to write to std::string?
- writing directly to std::string internal buffers
.. 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