Hi
The following program doesn't compile on MS VC++ or Bloodshed Dev-C++
#include <iostream>
int strlen(const char *in)
{
int count=0;
while (*in++) count++;
return count;
}
int main()
{
std::cout << "the length of string \"hey diddle diddle\" is ";
std::cout << strlen("hey diddle diddle") << '\n';
}
I always thought that using <iostream> rather than <iostream.h> would
keep standard library definitions out of the global namespace. Am I
incorrect in this assumption, or do those two compilers really suck
that bad? TIA
Despite quite a few name-calling, mud-slinging responses to the
contrary, there are things wrong with both your assumptions and your
program.
First, <iostream> is a standard header. <iostream.h> is not, and the
C++ standard says nothing at all about what it will or will not do.
Second, your program is invalid because it intrudes on names reserved
to the implementation. Here is what the C++ standard actually says:
[begin quotation]
17.4.3.1.3 External linkage
1 Each name declared as an object with external linkage in a header is
reserved to the implementation to designate that library object with
external linkage,166) both in namespace std and in the global
namespace.
2 Each global function signature declared with external linkage in a
header is reserved to the implementation to designate that function
signature with external linkage.167)
3 Each name having two consecutive underscores (2.11) is reserved to
the implementation for use as a name with both extern "C" and extern
"C++" linkage.
4 Each name from the Standard C library declared with external linkage
is reserved to the implementation for use as a name with extern "C"
linkage, both in namespace std and in the global namespace.
5 Each function signature from the Standard C library declared with
external linkage is reserved to the implementation for use as a
function signature with both extern "C" and extern "C++" linkage,168)
or as a name of namespace scope in the global namespace.
[end quotation]
I think I can make a case that your program violates all but paragraph
3.
The names of all standard C library functions are reserved with
external linkage in both the global and standard namespaces, and even
without external linkage are reserved at namespace scope in both
namespaces.
So the compilers you tried are quite within their rights to reject
your code.