using
The using keyword does four unrelated jobs: it imports a whole namespace, imports one specific name, declares a type alias, and inherits a base class's constructors. Only the spelling is shared.
Detailed Explanation
Reading C++ means recognising which of the four using you are looking at. They are distinguished entirely by what follows the keyword.
using namespace N; — the using-directive. Every name in N becomes visible for unqualified lookup in the current scope. using namespace std; is the one every tutorial opens with, and it is the one most likely to hurt you. It does not import names into the scope; it makes them candidates during lookup, which means they compete with your own names in overload resolution. Write your own count(container) and one day it becomes ambiguous with std::count. Worse, the ambiguity appears when someone adds an include, in a file you did not touch. Inside a small function body, a using-directive is defensible. At file scope in a source file it is a smell. In a header it is a genuine bug, because you have imposed the entire standard library namespace on everyone who includes you, transitively, forever.
using std::swap; — the using-declaration. This brings in one name rather than everything, and it does introduce a real declaration into the current scope. It is the honest version of the directive: narrow, explicit, greppable. The standard swap idiom relies on it — declare using std::swap; then call swap(a, b) unqualified so argument-dependent lookup can prefer a type's own overload while std::swap remains the fallback.
using Key = std::pair<std::string, int>; — the type alias. This replaces typedef and is better in two ways. First, it reads left to right, which matters as soon as function pointers are involved: compare typedef int (*Handler)(const Event&, int); with using Handler = int (*)(const Event&, int);. The alias puts the new name where your eye expects it.
Second, and this is the reason typedef is effectively obsolete, aliases can be templated. typedef cannot. Before C++11, a parameterised alias required a struct wrapper with a nested type member and a typename at every use site — the whole typename Alloc<T>::type dance that clutters older template code. An alias template is one line and needs no typename at the point of use, because it is a type by construction. The standard library's std::enable_if_t, std::remove_cv_t, and the rest of the _t suffixes are alias templates added in C++14 for exactly this reason.
using Base::Base; — inheriting constructors, C++11. If you derive HttpError from std::runtime_error and add no state, you still had to hand-write a forwarding constructor for every base constructor before C++11. One line now makes the base's constructors available as constructors of the derived class. The same syntax without parentheses, using Base::process;, does something related but distinct: it undoes name hiding. Declaring any overload of process in a derived class hides all base overloads, and a using-declaration brings them back into the candidate set.
One consequence worth remembering: using at class scope also changes access. A protected base member re-declared with using in the public section of a derived class becomes public, and vice versa.
Code Examples
#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
// Type aliases: read left to right, and they can be templated.
using UserId = std::uint64_t;
using SessionMap = std::unordered_map<UserId, std::string>;
using EventHandler = int (*)(const std::string&, int); // clearer than the typedef form
template <typename T>
using Owned = std::unique_ptr<T>; // alias template: impossible with typedef
template <typename T>
using Batch = std::vector<std::vector<T>>;
Owned<SessionMap> makeCache() { return std::make_unique<SessionMap>(); }
Batch<int> grid; // std::vector<std::vector<int>>#include <stdexcept>
#include <string>
class HttpError : public std::runtime_error {
public:
using std::runtime_error::runtime_error; // inherit all base constructors
// Pre-C++11 you wrote this by hand:
// explicit HttpError(const std::string& w) : std::runtime_error(w) {}
// explicit HttpError(const char* w) : std::runtime_error(w) {}
};
struct Base {
void process(int);
void process(const std::string&);
};
struct Derived : Base {
using Base::process; // without this, the int and string overloads are hidden
void process(double);
};
void demo() {
Derived d;
d.process(42); // resolves to Base::process(int) thanks to the using-declaration
throw HttpError("404 not found");
}Common Gotchas
`using namespace std;` in a header
every translation unit that includes the header inherits it, so a name collision can appear in a file whose author never opted in. Fully qualify names in headers.
A using-directive does not resolve ambiguity, it creates it
if both your namespace and `std` supply a viable `distance`, the unqualified call is ill-formed. Nothing warns you until a new include changes the candidate set.
Prefer `using` over `typedef` everywhere in new code
identical semantics for the non-template case, better readability, and the only form that supports alias templates.