Back to results for “Namespaces”
C++ › Namespaces

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

Intermediate

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.

Common follow-ups: Which is more precise? | Why prefer using-declarations?