468,512 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Is overriding a function of a library in accordance with C++ standard?

Is overriding a function of a library in accordance with C++ standard?

The following code are passed by the VS 2005 and Dev C++.

#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.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?

Any help will be appreciated. Many thanks in advance.

Aug 25 '06 #1
7 5367
Lighter wrote:
Is overriding a function of a library in accordance with C++ standard?
You can't override an existing function because that is violation to the
One Definition Rule (ODR.)

More explanation below.
>
The following code are passed by the VS 2005 and Dev C++.

#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.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?
Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}
>
Any help will be appreciated. Many thanks in advance.
Regards,
Ben
Aug 25 '06 #2
benben wrote:
Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}
Dev C++ can still normally compile the code, but VS 2005 report an
error.

Aug 25 '06 #3
"Lighter" <cq****@gmail.comwrote in message news:11**********************@p79g2000cwp.googlegr oups.com...
Is overriding a function of a library in accordance with C++ standard?

The following code are passed by the VS 2005 and Dev C++.

#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.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?
I am not sure what the standard says about it. In practice this works except
if other functions in the run-time library are used in the program.
On many platforms, the run-time functions are pre-linked.
(Linux uses shared object files (.so) .
Windows uses dynamic linked libraries (.dll)
VMS uses shareable images (.exe))
In such cases the standard version of the function is present in the pre-linked
file and will be used internally.
In order to use your own version of the function, special link options must
be use to prevent the use of the pre-linked run-time library. This will make
the resulting executable file much larger, as it now contains all run-time
library functions used by the program. In addition these functions are no
longer shared with other programs, so that the program may use more memory.
Aug 25 '06 #4
neo
I am using vs2k5. Your code is working perfectly; in your code I
include iostream.h in your code, its give error as following:

e:\vc\bug\bug\bug.cpp(12) : error C2883: 'std::strlen' : function
declaration conflicts with 'strlen' introduced by using-declaration
c:\program files\microsoft visual studio
8\vc\include\string.h(80) : see declaration of 'strlen'

If I want to use iostream.h, how we would be override strlen function?

In my understanding strlen function will not be override because it
will be conflict between two functions in std namespace, if it is
possible to override std function, I am interested to learn it.

Regards,
-aims
benben wrote:
Lighter wrote:
Is overriding a function of a library in accordance with C++ standard?

You can't override an existing function because that is violation to the
One Definition Rule (ODR.)

More explanation below.

The following code are passed by the VS 2005 and Dev C++.

#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.

int main(int argc, char *argv[])
{

system("PAUSE");

return EXIT_SUCCESS;
}

There is even no warning after compiling the code. In front of the
fact, I have to make a guess that all the C++ compilers are conformed
to the following rules:

1) The compiler first compiles all the source file included in the
project into object files;

2) At link time, the compiler first searches the object files for all
the unresolved symbols; if it fails to find some symbols, then the
compiler will search the libraries which are included in the project to
find the symbols.

3) If the object files containes a symbol, then the symbols that have
the same name in the libraries will be ignored.

Am I correct?

Nope. The standard library strlen resides in the std namespace. The
using directive merely makes such a name available in the global scope.
Your own version of strlen is declared in the global scope and so hides
the std::strlen.

To truly override strlen and get a compiler error you can do the following:

#include <cstring>

namespace std{
size_t strlen(const char*){return 0;}
}

int main()
{
strlen("oops!");
}

Any help will be appreciated. Many thanks in advance.

Regards,
Ben
Aug 25 '06 #5
neo wrote:
I am using vs2k5. Your code is working perfectly; in your code I
include iostream.h in your code, its give error
That's to be expected; its <iostream.his not a standard C++ header
but a microsoft-specific one to help their customers deal with
pre-standard
C++ code. Any error you get with it has nothing to do with standard
C++, the topic here. Try the microsoft.* newsgroups.

HTH,
Michiel Salters

Aug 25 '06 #6
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.
Aug 25 '06 #7
neo wrote:
I am using vs2k5.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian (free pizza mellows the engineer)
Aug 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by James Marshall | last post: by
15 posts views Thread by Susan Baker | last post: by
6 posts views Thread by Eric | last post: by
21 posts views Thread by sks | last post: by
12 posts views Thread by pantagruel | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.