Topic: Is overriding a function of a library in accordance with C++ standard?
Author: "SuperKoko" <tabkannaz@yahoo.fr>
Date: Sat, 26 Aug 2006 15:28:11 CST Raw View
Lighter wrote:
> Is overriding a function of a library in accordance with C++ standard?
>
> The following code is 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;
> }
>
BTW this is not a question for comp.std.c++ (comp.lang.c++ is more
appropriate).
It is not overriding (overriding involve inheritance & virtual
functions)... It is hiding... In fact, it might well be a name conflict
or any other bad thing (behavior is undefined).
"
17.4.3.1 Reserved names [lib.reserved.names]
3 If the program declares or defines a name in a context where it
is
reserved, other than as explicitly allowed by this clause, the
behav-
ior is undefined.
"
"
17.4.3.1.3 External linkage [lib.extern.names]
5 Each function signature from the Standard C library declared
with
external linkage is reserved to the implementation for use as a
func-
tion signature with both extern "C" and extern "C++" linkage,25) or
as
a name of namespace scope in the global namespace.
"
Your function signature is the same as strlen, and has extern "C++"
linkage in global namespace.
It is reserved for the implementation!
Behavior is undefined!
> There is even no warning after compiling the code.
Behavior is undefined... It might vary from a warning or error at
compile-time to behaving as you expected without any error or warning.
> 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?
No.
With most compilers (that's not required by the standard) all the
symbol detection is done at compile-time... And there is some symbol
mangling which avoids conflicts at link-time of symbols living in
different namespaces or having different signatures.
All that stuff is non-standard and you can't rely on it.
The only thing that the standard says about your program is that it has
undefined behavior... That is : The standard imposes no limitation on
the behavior that your program can have...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: kuyper@wizard.net
Date: Sat, 26 Aug 2006 22:07:04 CST Raw View
SuperKoko wrote:
> Lighter wrote:
> > Is overriding a function of a library in accordance with C++ standard?
.
> BTW this is not a question for comp.std.c++ (comp.lang.c++ is more
> appropriate).
I'm not sure I follow that. How could a question about whether the C++
standard allows something not be on-topic for comp.std.c++?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Lighter" <cqulyx@gmail.com>
Date: Fri, 25 Aug 2006 09:59:13 CST Raw View
Is overriding a function of a library in accordance with C++ standard?
The following code is 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.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]