Suppose you define a function inside the translation unit with the name of function exactly matching with any of the standard library function. As the compiler first looks for the definition in the translation unit(s) and then in the library file, so will this cause to use the own version of the function definition or will it cause a diagnostic?
-
5This sounds like a symptom of using `using namespace std;`. If you are using that [please stop](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). – NathanOliver Aug 03 '16 at 13:56
-
If you use your own namespace, you can reuse stl function names. e.g. `mynamespace{ ostream& cout(String str) ... }`. Then you can use your namespace using the scope resolution operator (::) to specify which cout you would like to use – pjcognetta Aug 03 '16 at 13:58
-
"Exactly matching" as in you're introducing new symbols in the `std` namespace? That's explicitely forbidden by §17.6.4.2.1: *"The behavior of a program is undefined if it adds declarations or definitions to namespace `std`"*. However, you're allowed to specialize templates on user types. – peppe Aug 03 '16 at 14:00
3 Answers
I'm not sure if I understand the question correctly (correct me if I am wrong) but say you have a function like so:
void sort( // blah blah
Or declaring the std namespace using namespace std
(which you should not be doing)
When you call sort(...)
within your translation unit, the compiler overloads the definitions and in this case your function has precedence.
But if you are calling the standard library function by explicitly defining the std namespace (using the scope resolution operator) like so
std::sort( // blah blah
it will use the std library function instead.
-
@HolyBlackCat I don't know why `putchar` has this behavior, but to me your code should not work, see http://ideone.com/EXJ50I. – Holt Aug 03 '16 at 14:12
-
1The standard library has no special precedence. If the `sort` from `std` is pulled in with `using namespace std` and there is also a user-defined function named `sort` the usual overloading rules apply. – Pete Becker Aug 03 '16 at 14:36
-
@ialcuaz - no, that's not right. The compiler handles overloading at the point of the call, and looks at all of the declarations that are in scope. It doesn't matter how they got there. – Pete Becker Aug 03 '16 at 14:48
-
1@ialcuaz - that example with `putchar` relies on the declaration of the C library functions being in the global namespace; there's no ambiguity because it amounts to `int putchar(int); int putchar(int i) { ... }` and the using declaration doesn't really affect anything. That's different from the C++ library functions, which are declared in namespace `std`. so in the example by @Holt there are **two** functions in the same scope: `std::stod`, which got hoisted into the global namespace by the using declaration, and the definition of `stod` in the global namespace. It's also different ... – Pete Becker Aug 03 '16 at 15:18
-
1... from an implementation that puts the C library functions in `std` and then hoists them into the global namespace (not that anyone does that, although it's legal to do it that way). The `putchar` example would be ambiguous with that implementation. All in all, it's an interesting example. – Pete Becker Aug 03 '16 at 15:19
No, the names of functions within namespace std
are not reserved - at least, not in the sense that using the same names outside of namespace std
is forbidden.
However, placing names within namespace std
(except in a few cases, such as specialising certain template functions) gives undefined behaviour.
If you are getting the compiler complaining about ambiguity when calling your functions, where one of the functions has the same name as yours but is in namespace std
, then the cause is probably a using namespace std
in your code. The effect of that, when the compiler encounters a name, is that both your functions and those in namespace std
are valid matches. If the compiler has no reason to prefer one over the other (e.g. they accept the same types of argument) the code will not compile. In that case, the solution is to remove the using namespace std
from your code - there is no way to undo the effects of using namespace std
other than removing it.

- 35,646
- 4
- 32
- 74
That's the reason why people say using namespace std;
in C++ is a bad practice.
Because the function definition gets conflicted when you use the same function name which is already defined inside the namespace std
leading to many unrelated errors.
Read this thread for more details:-
Why using namespace std; in C++ is considered a bad practice?

- 367
- 5
- 14