Question

I was surprised to see in this question that someone modified a working snippet just because, as the author of the second answer says:

it didn't seem appropriate to me that I should work with binary data stored within std::string object

Is there a reason why I should not do so?

Was it helpful?

Solution

For binary data in my opinion the best option is std::vector<unsigned char>.

Using std::string while technically works sends to the user the wrong message that the data being handled is text.

On the other side being able to accept any byte in a string is important because sometimes you know the content is text, but in an unknown encoding. Forcing std::string to contain only valid and decoded text would be a big limitation for real world use.

This kind of limitation is one of the few things I don't like about QString: this limitation makes it impossible for example to use a file selection dialog to open a file if the filename has a "wrong" (unexpected) encoding or if the encoding is actually invalid (it contains mistakes).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top