Just for what it's worth, there are a few things you can do by pulling a namespace with a using directive that you can't do by qualifying names. The canonical example is probably writing a generic sort function. If a swap
has been defined for the type being sorted, you want to use that, but if it doesn't have a swap
of its own, you want to use std::swap
.
To accomplish this, you can write your code like:
using namespace std;
// ...
template <class T>
void my_sort(std::vector<T> &x) {
// ...
if (x[j] < x[k])
swap(x[j], x[k]);
Now, if there's a swap
in the namespace of whatever type is being sorted, it'll be found by argument dependent lookup. If there's not, std::swap
will be found because you've made it visible with the using directive.