std::string as C++ byte array

c++

Solution

`std::string`s may have a reference counted implementation which may or may not be a advantage/disadvantage to what you're writing -- always be careful about that. `std::string` may not be thread safe. The potential advantage of `std::string` is easy concatenation, however, this can also be easily achieved using STL.

Also, all those problems in relation to protocols dissapear when using `boost::asio` and it's buffer objects.

As for drawbacks of `std::vector`:

- fast assign can be done by a trick with `std::swap`

- data can be accessed via &arr[0] -- vectors are guaranteed (?) to be continious (at least all implementations implement them so)

Personally I use `std::vector` for variable sized arrays, and `boost::array` for static sized ones.

Problem

Google's Protocol buffer uses the C++ standard string class `std::string` as variable size byte array (see here) similar to Python where the string class is also used as byte array (at least until Python 3.0). This approach seems to be good: - It allows fast assignment via `assign` and fast direct access via `data` that is not allowed with `vector<byte>` - It allows easier memory management and const references, unlike using `byte*`. But I am curious: Is that the preferred way for a byte arrays in C++? What are the drawbacks of this approach (more than a few `static_cast`s)

Original source