Is it reasonable to use std::basic_string as a contiguous buffer when targeting C++03?

前端 未结 5 486
挽巷
挽巷 2020-11-27 06:59

I know that in C++03, technically the std::basic_string template is not required to have contiguous memory. However, I\'m curious how many implementations exist

5条回答
  •  温柔的废话
    2020-11-27 07:38

    Edit: You want to call &buffer[0], not buffer.data(), because [] returns a non-const reference and does notify the object that its contents can change unexpectedly.


    It would be cleaner to do buffer.data(), but you should worry less about contiguous memory than memory shared between structures. string implementations can and do expect to be told when an object is being modified. string::data specifically requires that the program not modify the internal buffer returned.

    VERY high chances that some implementation will create one buffer for all strings uninitialized besides having length set to 10 or whatever.

    Use a vector or even an array with new[]/delete[]. If you really can't copy the buffer, legally initialize the string to something unique before changing it.

提交回复
热议问题