In article <11**********************@p79g2000cwp.googlegroups .com>,
cq****@gmail.com says...
Is overriding a function of a library in accordance with C++ standard?
Sometimes. Just for example, you are specifically allowed to supply your
own memory management functions. There are rather tight restrictions on
what you can add to namespace std though.
#include <cstdlib>
#include <iostream>
using namespace std;
size_t strlen(const char* p)
{
return 0;
} // !!! Note this !!! The standard library function strlen is
deliberately overriden.
Only sort of. The standard library has a function named "std::strlen".
If you include <cstringthat's how it's visible. If you include
<string.h>, it's been made visible in the global namespace (as if by
"using std::strlen"), but its name is still "std::strlen", not
"::strlen".
As such, what we have are two different functions with two different
names, but a mechanism (or two, technically -- the using declaration and
the using directive) that allows us to ignore that particular difference
in the name.
That means your name doesn't directly collide with the one in the
standard library. You're not allowed to do this though:
namespace std {
size_t strlen(const char *p) {
return 0;
}
};
Now, one other minor detail: in C++, any standard header is allowed to
include any other standard header. That means that even though you
haven't included either <cstringor <string.h>, it's possible that
having included <iostreamand <cstdlibcould include <cstringor
<string.h>. In that case, the name in your code would collide with the
name from the standard library, and the compiler would reject your code.
Most of the rest of your summary about how compilation works is
generally correct in general, but not required -- and while most
compilers work about as you described, there are a few that work
substantially differently (e.g. the last time I looked at it, IBM VA C++
took a somewhat more global approach to things. I never tested it with
code like yours but I wouldn't be surprised if it reacted somewhat
differently to your code).
To summarize: the compiler is allowed but not required to reject your
code. As an aside, I'd also add that C has substantially stricter rules
in this respect than C++. In C all names starting with 'str' are
reserved, so code like your's would have undefined behavior.
--
Later,
Jerry.
The universe is a figment of its own imagination.