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.
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 stdReal-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