Advertisement
Intermediate C++, Namespaces

What is a using-declaration versus a using-directive?

Short answer

A using-declaration (using std::vector;) brings a single name into scope. A using-directive (using namespace std;) brings all names from a namespace into scope. Using-declarations are more precise and safer; using-directives are broad and collision-prone.

Save C++ mock interview
Advertisement
AModel answer
A using-declaration (using std::vector;) brings a single name into scope. A using-directive (using namespace std;) brings all names from a namespace into scope. Using-declarations are more precise and safer; using-directives are broad and collision-prone. Prefer using-declarations, scoped as narrowly as possible.
using std::string;      // declaration: just string
using namespace std;    // directive: everything in std
Real-world example
A function uses a targeted using-declaration for std::swap to enable ADL while keeping scope clean.
Likely follow-ups
  • Which is more precise? | Why prefer using-declarations?
Related topics