Consider this library header:
#include<vector>
#include<algorithm>
#include<iostream>
namespace Lib {
namespace detail {
using namespace std;
template<class T>
void sort_impl(istream &in,ostream &out) {
vector<T> v;
{
int n;
in >> n;
v.resize(n);
}
for(auto &i : v) cin >> i;
sort(v.begin(),v.end());
for(auto i : v) out << i << endl;
}
}
inline void sort_std() {
detail::sort_impl<int>(std::cin,std::cout);
}
}
Does the detail
namespace successfully isolate the clients of the library (and the rest of library's implementation) from the using-directive in this example? I'm not interested in the discussion at Why is "using namespace std" considered bad practice?, even though some of the arguments apply even to "well contained" using-directives.
Note that there are two existing questions concerning the same situation but with using-declarations:
- Using declarations in private namespaces in header files
- Elegant way to prevent namespace poisoning in C++ (whose one answer is really an answer to the "bad practice" question above)
This could be combined with either of them, but the editing would be severe.
You pollute your own
detail
namesapce, but not theLib
or global namespaces. So assuming a responsible adult is using your library, they won't have unintentional name collisions: