In C++, NULL is zero. [...] Therefore, I reason that NULL == '\0' will always hold true.
No, it's not and it will not. 18.1/4 says:
The macro NULL is an implementation-defined C++ null-pointer constant in this International Standard.
It is therefore valid for a compiler to define NULL to be something other than 0, provided that it still is assignable to any pointer type and will result in the null pointer value.
In particular, take GCC. It has a compiler extension keyword __null. This special keyword is convertible to any null pointer or the integer 0, but on the latter conversion the compiler emits a warning. Thus, given the compiler command line
g++ -Werror null.cpp
with null.cpp:
Code:
#include <cassert>
#include <cstdlib>
int main()
{
assert(NULL == '\0');
}
the assertion will not hold - the program will not compile.
More to the point, many C++0x implementation will (perhaps optionally) implement NULL as a typedef for nullptr, where the above program will result in an error even without treating warnings as errors.
NULL is to be treated as a pointer and nothing else. In particular, it is definitely not to be treated as a character.
By the way:
Code:
namespace {
template <typename Ch>
Ch *ustrstr_impl(Ch *haystack, Ch *needle)
{
for(Ch *ho = haystack; *ho; ++ho) {
const char *hi, *ni;
for(hi = ho, ni = needle; *hi && *ni && *hi == *ni; ++hi, ++ni) {
}
if(!*ni) return ho;
}
return 0;
}
}
const char *ustrstr(const char *haystack, const char *needle)
{
return strstr_impl(haystack, needle);
}
char *ustrstr(char *haystack, char *needle)
{
return strstr_impl(haystack, needle);
}
const wchar_t *uwcsstr(const wchar_t *haystack, const wchar_t *needle)
{
return strstr_impl(haystack, needle);
}
wchar_t *uwcsstr(wchar_t *haystack, wchar_t *needle)
{
return strstr_impl(haystack, needle);
}
... because you don't want to carry over C's limitations to your implementation, do you?