3

Possible Duplicate:
Do you prefer explicit namespaces or 'using' in C++?

I am a C# developer, but my friend is a C++ one. He has shown me the code which is filled with calls like std::for_each and boost::bind. I used in C# and thought that using directives would rock for readability of the code and generally faster development. It would be a pain in the neck to type any namespace before C# foreach statement for example.

What are the cons and pros of using for such popular namespaces I am wondering?

Is it a best practice to include those namespaces or not?

peterh
  • 11,875
  • 18
  • 85
  • 108
Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
  • Possible duplicate http://stackoverflow.com/questions/214927/do-you-prefer-explicit-namespaces-or-using-in-c – Kos Dec 12 '10 at 21:30
  • 1
    There's loads of dupes. I don't think that question is a good one, as there are few answers the other way. – Puppy Dec 12 '10 at 21:31
  • 2
    See http://stackoverflow.com/questions/1265039/using-std-namespace/1265092#1265092 – icecrime Dec 12 '10 at 21:31
  • well I am asking what is best practice, I can see benefits of explicit std:: from that post, but is it a real best practice across serious C++ projects or not? I can see 27 rating, but I can also see a lot of people saying using for std and boost are fine and great.. – Valentin Kuzub Dec 12 '10 at 21:41
  • For example he says that using std might create problems if user wants to define a vector class, but one could say that creating classes with same names as STD classes is bad practice. Only real problem is count I think, because it can be used in any user created class.. Now is it a big enough reason to type std:: everywhere else to avoid conflit with STD count? – Valentin Kuzub Dec 12 '10 at 21:51
  • @Valentin: In my experience, it *is* a serious best practice, particularly in header files. You have a certain amount of leeway, though, as long as you make sure to never pollute the global namespace in a header. – Stuart Golodetz Dec 12 '10 at 21:53
  • @Valentin: no, creating classes/functions with names that clash with those in the standard library is not bad practice. That's **exactly** why they were put in the `std::` namespace in the first place. – André Caron Dec 12 '10 at 23:32
  • I understand this fully. But best practices at the time when std:: was created and was very new library and not many people were good with have passed, and now its a basic tool of most C++ developers, so time has changed and imo if someone implements his own vector today he should think 3 times whether he really wants to name it same as std::vector. Disagree? – Valentin Kuzub Dec 12 '10 at 23:57
  • @André: All the same, I'd personally tend to avoid creating e.g. my own class template called `vector` unless it was functionally my own substitute for `std::vector` (for some reason). Writing a mathematical vector class called `vector` and then potentially writing nasty things like `std::vector` doesn't appeal to me in any way. That sort of thing *is* bad practice in my opinion. The same doesn't go for names in `std` in general, though -- it's a case of exercising good judgement. – Stuart Golodetz Dec 13 '10 at 10:01
  • @Valentin: if the intent is to be a substitute, I would *encourage* using the same name. However, that's not what I was talking about, see my next comment. – André Caron Dec 13 '10 at 17:43
  • @Stuart: my point is that if a name use by a standard library class/function is appropriate for your class/function, you should not try to avoid it *just because* it is used by the standard library. If I was writing a 3D graphics library with classes `point` and `matrix`, not using ` vector` would most likely create more confusion than the odd appearance of `std::vector>`. – André Caron Dec 13 '10 at 17:48
  • @André: Well I agree with you in the general case. In the specific case mentioned I'd probably call it `vec` or something though. – Stuart Golodetz Dec 13 '10 at 21:06

2 Answers2

10

First of all, let's make two distinctions:

1) There are using directives like using namespace std; and using declarations like using std::cout;

2) You can put using directives and declarations in either a header (.h) or an implementation file (.cpp)

Furthermore, using directives and declarations bring names into the namespace in which they're written, i.e.

namespace blah
{
    using namespace std; // doesn't pollute the *global* namespace, only blah
}

Now, in terms of best practice, it's clear that putting using directives and declarations in a header file in the global namespace is a horrible no-no. People will hate you for it, because any file that includes that header will have its global namespace polluted.

Putting using directives and declarations in implementation files is somewhat more acceptable, although it may or may not make the code less clear. In general, you should prefer using declarations to using directives in such instances. My own preference is to always specify the namespace, unless it's annoyingly long (then I might be tempted).

In a header, if typing the namespace every single time is getting really tedious, you can always introduce a "local" namespace, e.g.

namespace MyLocalName
{
    using namespace boost;

    class X
    {
        // can use things from boost:: here without qualification
    };
}

using MyLocalName::X; // brings X back into the global namespace

But never put using namespace boost; or the equivalent somewhere where it will drag all the stuff from Boost into the global namespace itself for other people.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • Header files are not in question. Its clear that they are not a good candidate for using. Question is - general C++ programmers view on this, I am sure some serious companies have coding standards, so I ask what do they say about this. I dont think its programmer dependant , not company dependant. – Valentin Kuzub Dec 12 '10 at 22:00
  • 1
    @Valentin: Well Google's style guide says something about it, for what it's worth: . Not that I agree with everything there, but it seems sensible enough on this one. – Stuart Golodetz Dec 12 '10 at 22:04
  • yeah they are pretty clear against it as I can see.. guess given information I received this can be called the end of story, gogo std::for_each ! – Valentin Kuzub Dec 12 '10 at 22:09
  • Glad it helped. Incidentally, I apologise to anyone else who might read this for the broken link in the above -- clearly the trailing angle bracket needs to be removed, sorry (I can't edit it any more). – Stuart Golodetz Dec 12 '10 at 22:13
  • Just to add to the answer. For long namespaces you can always use an alias. For example, "namespace bf = boost::filesystem; bf::file_size("test");" – daveraja Jun 23 '15 at 00:48
0

I am against the using namespace statements, except maybe locally in a function's body. It's not a pain at all to write the full-qualified namespace before every identifier, except if you're writing like 500 loc per day.

ognian
  • 11,451
  • 4
  • 35
  • 33
  • 2
    It's not that it's a pain to write -- it's that it can be a pain to read in some instances. As an example, fully-qualifying everything when creating a Boost multi-index container makes the resulting code hard-to-understand. Personally I think there's a very good case for `using` in that sort of instance, provided you don't pollute any namespaces you shouldn't be polluting. – Stuart Golodetz Dec 12 '10 at 22:10
  • 2
    In contrast I find difficult to read code that is stripped from namespaces, I even wonder where comes 'string' from, when it's not prefixed with 'std::' – ognian Dec 12 '10 at 22:13
  • @ognian: In general, I entirely agree with you -- I rarely use `using` in my own code. There are some situations in which I think it can be justified though. – Stuart Golodetz Dec 12 '10 at 22:23
  • Fully qualifying all names mostly become painful with long namespace names, or deep (read: 2+ level) namespace nesting. I don't mind typing `std::vector` all the time, but if it were `standard::containers::vector`, it would make me *highly* inclined to put `using` directives all over the place. – André Caron Dec 13 '10 at 17:52
  • @André: good point, especially saying 'all over the place' - I would put `using` directives only in function bodies, where long namespaces are used more than twice. – ognian Dec 13 '10 at 18:01
  • @ognian: that's what I usually do :-) – André Caron Dec 13 '10 at 18:30