Chapter 3.6

SF.7: Don’t write using namespace at global scope in a header file

Don’t do this

Please, don’t do this. Ever.

Never. Not ever. Not even once.

Please.

The Core Guidelines document offers the following example:

// bad.h
#include <iostream>
using namespace std; // bad <- "You're not kidding," says Guy

// user.cpp
#include "bad.h"

// some function that happens to be named copy
bool copy(/*... some parameters ...*/);

int main()
{
  // now overloads local ::copy and std::copy, could be ambiguous
  copy(/*...*/);
}

“Bad” is an understatement. We know it seems we are belaboring the point, but this example only exposes part of the horror.

Disambiguation

There is one and only one global namespace. From the standard, ...

Get Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.