views:

120

answers:

4
+3  Q: 

namespace usage

I'm trying to start using namespaces the correct (or at least best) way.

The first thing I tried to do was to avoid putting using namespace xxx; at the beginning of my files. Instead, I want to using xxx::yyy as locally as possible.

Here is a small program illustrating this :

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
   using std::cout;
   using std::endl;

   srand(time(0));

   for(int i=0; i<10;++i)
      cout << rand() % 100 << endl;

   return 0;
}

If I omit the lines using std::cout; or using std::endl, the compiler will complain when I'm trying to use cout or endl.

But why is this not needed for srand, rand and time ? I'm pretty sure they are in std, because if I try to specifically pour std:: in front of them, my code is working fine.

+3  A: 

If you really want to know, take a close look at the ctime and cstdlib headers. They were built backwards-compatible.

Note: all this using vs. using namespace business is about readability. If your IDE would allow to just not show the namespaces when you don't want to see them, you wouldn't need these constructs...

xtofl
+1 for smarter IDEs...
Inverse
+3  A: 

I prefer to omit using and just have the std::cout every time just to maintain readability. although this is probably only useful in larger projects

Stowelly
I second this - I also prefer writing the full namespace everywhere (even for small projects).
laura
And if something is particularly unpleasant and used alot then typedef it to something shorter
Patrick
+6  A: 

If you use cstdlib et al. the names in them are placed in both the global and the std:: namespaces, so you can choose to prefix them with std:: or not. This is seen as a feature by some, and as a misfeature by others.

anon
\*cough\* misfeature \*cough\*
GMan
A: 

As long as we on the subject, there's also a thing called Koenig Lookup which allows you to omit a namespace identifier before a function name if the arguments it take come from the same namespace.

For example

#include <iostream>
#include <algorithm>
#include <vector>

void f(int i){std::cout << i << " ";}
int main(int argc, char** argv)
{
   std::vector<int> t;
   // for_each is in the std namespace but there's no *std::* before *for_each*
   for_each(t.begin(), t.end(), f); 
   return 0;
}

Well, it's not related directly but I though it may be useful.

Serge
I don't think it's guaranteed by the standard that `std::vector::iterator` is in the std namespace. It could be in some implementation-defined namespace, or it could be a raw pointer. So I realise this is only an example, but I don't think it's a good habit.
Steve Jessop
@Steve Jessop - I didn't say it's a good thing it's a thing we should be aware of :-)
Serge
Is that a result of koenig lookup. If the iterators were pointers to an array of int then it would fail? Which is another reason to be explicit about the std:: as you may end up with for_each() from some other jokers namespace.
Martin York
Well, if "some other joker" wrote the iterator, the theory is that they probably know how best to implement `for_each` for that iterator. It's only really `swap` that's commonly overloaded via ADL, to the point that it's basically wrong to call `std::swap` explicitly (on objects in general, I mean. Iterators you expect to be cheaply copyable). There are some iterator/algorithms combos which could in theory benefit, though: implementing `find` for `std::set::iterator` springs to mind, but for some reason the standard didn't go there.
Steve Jessop