The fact that std::string can actually hold '\0' characters comes up all the time. This is of course inconsistent with C-style strings.
So I’m wondering, is this by design, or is it an omission, or is it just the fact that standard doesn’t forbid it and compilers allow this to happen?
I’m wondering what your quarrel is.
'\0'is just another character. There is no efficient way to forbid it in a general purpose ‘char’ string.That the same character has a special meaning in C is unfortunate but has to be dealt with as every restriction that is imposed by legacy code as soon as you interoperate with it.
This shouldn’t be an issue as long as you stick to code that uses
std::stringexclusively.To address your comment we need to look at the constructor that takes a
char*which would bebasic_string(const charT* s, const Allocator& a = Allocator())in21.4.2 9/10in n3242. It says that the size of the internal string is determined throughtraits::length(s)which in the case ofstd::stringisstrlenwhich requires its argument to be null terminated. So yes, if you try to construct astd::stringfrom anconst char*it needs to be null terminated.