Say I have this piece of code.
#include <string>
int main()
{
std::string(0);
return 0;
}
Writing std::string(0)
results in std::basic_string<char>::basic_string(const char*)
being called, with 0
as the argument to this constructor, which tries to treat the argument as a pointer to a C-string.
Running this code obviously results in a std::logic_error
being thrown. But my question is this : why both GCC and MSVC 8.0 don't emit any warnings? I'd expect to see something along the lines of "Making pointer from an integer without a cast".
0
is an integer constant expression with value 0, so it is a null pointer constant. Using a 0-valued constant as a null pointer is not a cast.
C++11 introduces nullptr
(and nullptr_t
), but the treatment of 0
as a null pointer is unlikely to change in the near future as large amounts of code depends on it.