There seem to be different views on using 'using' with respect to the std namespace.
Some say use ' using namespace std
', other say don't but rather prefix std functions that are to be used with ' std::
' whilst others say use something like this:
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
for all the std functions that are to be used.
What are the pros and cons of each?
using namespace std
imports the content of thestd
namespace in the current one. Thus, the advantage is that you won't have to typestd::
in front of all functions of that namespace. However, it may happen that you have different namespaces that have functions of the same name. Thus, you may end not calling the one you want.Specifying manually which ones you want to import in
std
prevents that from happening, but may result in a long list of using at the beginning of your file, which some developer will find ugly ;) !Personally, I prefer specifying the namespace each time I use use a function, except when the namespace is too long, in which case I put some using at the beginning of the file.
EDIT: as noted in another answer, you should never put a
using namespace
in a header file, as it will propagage to all files including this header and thus may produce unwanted behavior.EDIT2: corrected my answer, thanks to Charles comment.
If you don't have a risk of name conflicts in your code with std and other libraries you can use :
But if you want know precisely the dependancy of your code for documentation or there is a risk of name conflicts use the other way :
The third solution, don't use these solutions and write std:: before each use in code brings you more security but, maybe a little heaviness in the code...
The only reason to leave off the std:: is that you could, in theory, reimplement all the STL functions yourself. Then your functions could be switched from using std::vector to my::vector without changing the code.
Much like in Java where you can use either can include java.util.* or simply select each class individually, it depends on style. Note that you don't want one
using namespace std
at the start of your file/wide scope because you will pollute the namespace and possibly have clashes, defeating the point of namespaces. But if you have a function that uses a lot of STL, it clutters the code to have a jumble of prefixing syntax in your logic and you should probably consider using eitherusing namespace std
(when using a variety of classes) or individualusing
s (when using a few classes often).Excluding the basics (Having to add std:: infront of all stl objects/functions and less chance of conflict if you don't have 'using namespace std')
It is also worth noting that you should never put
In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace.
In some cases it is very beneficial to use things like
As if there is a specialized version of swap, the compiler will use that, otherwise it will fall back on std::swap
If you call std::swap, you always use the basic version, which will not call the optimized version (if it exists).
Why not for example
instead of the unwieldy
I find that much more readable and its my standard for coding.
You can even use it to include some semantic information for the reader. For example, consider the function prototypes
which ones the return value?
How about instead