template <...>
inline char_type const *string_class<...>::c_str() const
{
return m_buffer != 0 ? m_buffer : "";
}
With C-strings, ie., char const *, an empty string can come in two forms,
the empty string (which is a pointer to an array of one character, with value
0), or it can be the null string (which is the null pointer). This is a serious
disparity. On many architectures, de-referencing the null pointer causes an access
violation, and it is in all cases undefined, so it is something we want to avoid.
The strategy I chose for the string access shims was to implement c_str_ptr to match the semantics of c_str(), hence it never returns a null pointer. What this means is that any algorithm that uses the c_str_ptr shims can rely on the fact that it will never be presented with a null string.
However, some uses of shims might require a NULL pointer to be returned when the string is empty. This is supported by the shim c_str_ptr_null. Each c_str_ptr() shim in the STLSoft libraries has a matching c_str_ptr_null() shim, some of which are shown in Listing 11.