Unfortunately (thanks C!) it is “possible” to construct a std::string
from the integer 0
, because it counts as a null pointer literal.
However, it’s not really possible:
Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by
s
. The length of the string is determined by the first null character. The behavior is undefined if[s, s + Traits::length(s))
is not a valid range (for example, ifs
is a null pointer).
You got a crash because you tried to create a std::string
from a null pointer.
At least with GCC the result of this contract violation is an [unhandled] exception with a descriptive name:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Unfortunately, though, you can’t rely on this, and you don’t get any warnings during build. ?
2
solved std::map