I am a C++ newcomer, trying to learn the language in parallel as I work on a project that requires it. I am using a fairly popular and stable open source library to do a lot of heavy lifting. Reading through the source, tutorials and code samples for the library, I have noticed that they always use fully qualified names when declaring types, which often results in very long and verbose lines with lots of ::'s. Is this considered best practice in C++? Is there a different way to deal with this?
-
1[google namespaces for C++](http://stackoverflow.com/questions/6955023/c-namespace-best-practice-dilemma) "using std::string" or "using namespace std" – Son-Huy Pham Jun 24 '13 at 17:18
-
Without seeing the code in question, this is impossible to answer. But a long seres of `::`-separated namespace names in front of a name is probably not right. – Pete Becker Jun 24 '13 at 17:32
-
Might this "fairly popular and stable open source library" be Boost by chance? – statueuphemism Jun 24 '13 at 17:39
-
@statueuphemism: Boost is not one library. – Lightness Races in Orbit Jun 24 '13 at 18:26
-
@Lightness Races in Orbit, thank you for the technical correction. I tend to think of it as one library since there is a versioned downloadable package of all Boost libraries and all of the linkage happens automagically. – statueuphemism Jun 24 '13 at 21:17
-
@statueuphemism: Yeah me too - I'm just being annoying for the lulz :-) – Lightness Races in Orbit Jun 24 '13 at 21:55
3 Answers
They may have found it easier than answering lots of questions from people who tried the example code and found it didn't work, just because they didn't "use" the namespaces involved.
Practices vary - if you're working on a large project with lots of diverse libraries and name clashes, you may wish to proactively use more namespace qualifiers consistently so that as you add new code you won't have to go and make old code more explicit about what it's trying to use.
Stylistically, some people prefer knowing exactly what's being referred to to potentially having to dig around or follow an IDE "go to declaration" feature (if available), while other people like concision and to see fuller namespace qualification only on the "exceptional" references to namespaces that haven't been included - a more contextual perspective.
It's also normal to avoid having "using namespace xxx;" in a header file, as client code including that header won't be able to turn it off, and the contents of that namespace will be permanently dumped into their default "search space". So, if you're looking at code in a header that's one reason they might be more explicit. Contrasting with that, you can having "using namespace" inside a scope such as a function body - even in a header - and it won't affect other code. It's more normal to use an namespace from within an implementation file that you expect to be the final file in a translation unit, compiling up to a library or object that you'll link into the final executable, or perhaps a translation unit that itself creates the executable.

- 102,968
- 15
- 177
- 252
First typedefs:
typedef std::vector<MyTypeWithLongName>::const_iterator MyTypeIt;
//use MyTypeIt from now on
Second "using"
using std::string;
//use string instead of std::string from now on
Third "using namespace"
using namespace std;
//Use all things from std-namespace without std:: in front (string, vector, sort etc.)
For the best practice: Don't use 'using' and 'using namespace' a lot. When you have to use it (sometimes keeps the code cleaner) never put it in the header but in the .cpp file. I tend to use one of those above if the names get really long or I have to use the types a lot in the same file.

- 2,234
- 16
- 18
-
-
Only for stl-types. These tend to get quite long as shown in the first example. I just wanted to count the possibilities, not rank them – Marius Jun 24 '13 at 17:48
-
2
-
3C++11 to the rescue! Now everyone can be happy with "using": http://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11 – statueuphemism Jun 24 '13 at 21:22
If you are writing your own libraries you will certainly have heavy use of namespaces, In your core application there should be fewer uses. As for doing something like std::string
instead of starting with using namespace std;
imo the first version is better because It is more descriptive and less prone to errors

- 18,343
- 7
- 63
- 78