best practice for using namespaces

using the namespace std for example. What is the best practice?

being the less keystrokes, but most dangerous to overwrite
1
2
#include <iostream>
using namespace std;


This one is probably my favorite (maybe because i am not fluent in c/c++) but i like how it explicitly states what is being used while at the same time not having to type std:: on anything stated below (which seems to be a lot)
1
2
3
4
5
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector

then also along with this one, i can put my includes with what namespace being used under it, so i know what namespace comes from what library.

This is what everyone says you "should" do, but it clutters up the code so much. Everywhere you have vectors and strings comes along with std::. It seems like it clutters up the code a lot.
std::cout << somevar << std::endl;

I was just wondering what you guys thought of each. When do you use each? And of course being that the second one is my favorite, is there an problem with using the second method?
Last edited on
I don't want to tell anyone what to do but personally I prefer the third option to put std:: everywhere. Now I'm so used to it that it makes the code easier to read because I can directly see if it's something from the standard library or not.
I agree with Peter, whats the point of a namespace if you're just gonna circumvent it with "using"
To me everything seems to be from std. Coming from Python, what is more confusing is something like <iomanip> where setprecision is using std::, my first thought would be that it would have some link to what library it is from, and not jsut a general std with numerous libraries.

something in python would be like:
1
2
import urllib
urllib.request.Request()

where urllib is part of the stdlib, but when using it urllib is prefixed everytime, so i know what library it came from.

in c/c++"
1
2
#include <iomanip> 
cout << std::setprecision(5);


how are you suppose to know that seprecision comes from the library iomanip? Aside from remembering, and googling.
Last edited on
how are you suppose to know that seprecision comes from the library iomanip? Aside form remembering, and googling.

How is one supposed to know anything aside from remembering and checking the documentation?
How is one supposed to know anything aside from remembering and checking the documentation?

true

It just seems like there would be an inclination as to what library something comes from making it easier to remember iomanip goes with setprecision, and etc. with all others
Last edited on
setprecision is an iostream manipulator. Isn't that easy enough to remember?
no not really. I just recently included iomanip to use std::fixed thinking that that was its library, but then reading someone elses code realized they used fixed without inlcuding that lib. So i was thinking something that more clearly states that confusion.

but i mean fixed manipulates iostream, so i would assume that it would be defined in iomanip, but guess not
Last edited on
I'd also vote for the third option.

You really eliminate any chance of encountering ambiguity errors that way.
@metulburr: fixed is defined under this class
http://www.cplusplus.com/reference/ios/ios_base/fmtflags/
Topic archived. No new replies allowed.