This question may be a duplicate, but I can't find a good answer. Short and simple, what requires me to declare
using namespace std;
in C++ programs?
This question may be a duplicate, but I can't find a good answer. Short and simple, what requires me to declare
using namespace std;
in C++ programs?
First of all, this is not required in C - C does not have namespaces. In C++, anything in the
std
namespace which includes most of the standard library. If you don't do this you have to access the members of the namespace explicitly like so:Technically, you might be required to use using (for whole namespaces or individual names) to be able to use Argument Dependent Lookup.
Consider the two following functions that use
swap()
.dumb_swap
always callsstd::swap
- even though we'd rather prefer usingzzz::swap
forzzz::X
objects.smart_swap
makesstd::swap
visible as a fall-back choice (e.g when called with ints), but since it doesn't fully qualify the name,zzz::swap
will be used through ADL forzzz::X
.Subjectively, what forces me to use
using namespace std;
is writing code that uses all kinds of standard function objects, etc.IMO, in code like this
std::
just makes for line noise.I wouldn't find
using namespace std;
a heinous crime in such cases, if it is used in the implementation file (but it can be even restricted to function scope, as in the swap example).Definitely don't put the using statement in the header files. The reason is that this pollutes the namespace for other headers, which might be included after the offending one, potentially leading to errors in other headers which might not be under your control. (It also adds the surprise factor: people including the file might not be expecting all kinds of names to be visible.)
All the files in the C++ standard library declare all of its entities within the std namespace.
e.g: To use
cin,cout
defined in iostreamAlternatives:
You should definitely not say:
in your C++ headers, because that beats the whole point of using namespaces (doing that would constitute "namespace pollution"). Some useful resources on this topic are the following:
1) stackoverflow thread on Standard convention for using “std”
2) an article by Herb Sutter on Migrating to Namespaces
3) FAQ 27.5 from Marshall Cline's C++ Faq lite.
Firstly, the
using
directive is never required in C since C does not support namespaces at all.The
using
directive is never actually required in C++ since any of the items found in the namespace can be accessed directly by prefixing them withstd::
instead. So, for example:is equivalent to:
Whether or not you choose to use it is a matter of preference, but exposing the entire
std
namespace to save a few keystrokes is generally considered bad form. An alternative method which only exposes particular items in the namespace is as follows:This allows you to expose only the items in the
std
namespace that you particularly need, without the risk of unintentionally exposing something you didn't intend to.Namespaces are a way of wrapping code to avoid confusion and names from conflicting. For example:
File common1.h:
Usage file:
So, when you write
using namespace std
all you are doing is telling the compiler that if in doubt it should look in thestd
namespace for functions, etc., which it can't find definitions for. This is commonly used in example (and production) code simply because it makes typing common functions, etc. likecout
is quicker than having to fully qualify each one asstd::cout
.