Does anyone know what the behaviour of std::string.assign(NULL) is? Assigning NULL (using operator) or constructing from NULL is undefined. Does the same apply for this function?
2 Answers
The standard says this in 21.4.6.3 paragraph 12:
Requires: s points to an array of at least traits::length(s) + 1 elements of charT.
In the character traits requirements Table 62 is says:
X::length(p) std::size_t yields: the smallest i such that X::eq(p[i],charT()) is true. linear
That implies that traits::length() will dereference s and, thus, s can't be a pointer to null. You get undefined behavior.
2 Comments
It's undefined behavior to assign a C++ string from a NULL "C string", because a null pointer is not actually pointing to a C string at all. I could find no reference saying that std::string should check for NULL--you need to do it yourself.