What does 'using namespace std;' do and why is it discouraged in headers?
Short answer
using namespace std; brings all names from std into the current scope so you can omit the std:: prefix. It's discouraged, especially in headers, because it pollutes the global/namespace scope everywhere the header is included, risking name collisions and ambiguities.
Advertisement
AModel answer
using namespace std; brings all names from std into the current scope so you can omit the std:: prefix. It's discouraged, especially in headers, because it pollutes the global/namespace scope everywhere the header is included, risking name collisions and ambiguities. Prefer explicit std:: or targeted using-declarations in narrow scopes.
using std::cout; // targeted, safer
// avoid: using namespace std; in headersReal-world example
A using namespace std in a widely-included header causes a surprising ambiguity with a user 'count' function.
Likely follow-ups
- Why is it worse in headers? | What is a safer alternative?
Related topics