Talk:cpp/string/basic string/operator at
From cppreference.com
[edit] .size() (expanded via resize()) vs .capacity() (expanded via reserve()):
This page states: "If pos > size(), the behavior is undefined.". What about calling `.reserve()` on the string to reserve storage space? Does that also work? It seems to be that if `capacity()` is sufficiently large--not just `size()`, then you could write into those bytes too, no? ERCaGuy (Gabriel Staples) (talk) 17:09, 2 June 2022 (PDT)
- I did a bunch of testing. .reserve() is not sufficient. See my answer here: https://stackoverflow.com/a/72485404/4561887 and test code here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/cpp/string__use_std_string_as_a_c_str_buffer.cpp. So, the page is correct as-is, stating that .size() must match. ERCaGuy (Gabriel Staples) (talk) 23:22, 2 June 2022 (PDT)