Reputation: 1750
I wanted to know how does the compiler/linker selects from the 2 printf functions available. One is the user defined and the other is the standard c library implementation.
#include <stdio.h>
int printf(const char* c, ...) {
return 0;
}
int main() {
printf("\n Hello World\n");
}
I know what overloading is but here both implementations have same signatures. Basically I don't understand this concept of 'overriding' of functions. Does this somehow violate ODR? Is this a well defined C++ program or can it have UB on some platforms?
Upvotes: 2
Views: 670
Reputation: 119847
Overriding is a completely different concept from overloading. You override a virtual member function.
No overloading takes place here.
What actually happens is you are defining printf
with the exact same signature as stdio.h
declares. So it's the same function (with "C" linkage!). You are just providing its definition. It is undefined behaviour to define a standard library function, except for those functions explicitly mentioned as user-replaceable.
Upvotes: 2
Reputation: 22152
The program has undefined behavior because it defines a name in a reserved context. [extern.names]/4 of the C++17 standard draft states that function signatures with external linkage from the C standard library are reserved and [reserved.names]/2 says that defining a name in a reserved context causes undefined behavior.
Upvotes: 1