0

Consider the following C++11 program:

 #include <stdhdr1>
 #include <stdhdr2>
 #include <stdhdr3>

 using namespace std;

 #include <boost1>
 #include <boost2>
 #include <boost3>

 int main()
 {
 }

Where stdhdrnis a standard library header, and boostn is a boost header.

Is there ever a situation where this won't compile?

Furthermore given some additional user code in main, is there ever an ambiguity (or other error) caused by opening the std namespace that cannot be resolved by explicitly qualifying the ambiguous (or erroneous) entity?

(For example because of ADL considering the open std namespace or otherwise?)

(Put another way, can boost handle being included after an open std namespace?)

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • It does, obviously, beg the question: why not just put the `using namespace` after the boost includes? – nneonneo Feb 21 '13 at 19:26
  • 14
    Why not remove `using namespace std;` altogether and forget about it? – GManNickG Feb 21 '13 at 19:26
  • 5
    Simple answer, don't `using namespace` at all. – 111111 Feb 21 '13 at 19:27
  • 4
    That doesn't inspire much confidence in those codebases. – juanchopanza Feb 21 '13 at 19:34
  • Quite a number of large codebases open std in the global header to save on boilerplate code but there is a discussion on whether or not it is good or bad practice [here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c). – Andrew Tomazos Feb 21 '13 at 19:52

1 Answers1

4

I don't believe there would be a problem since std does not contain a namespace called boost, so there still won't be any name clashes.

As for ADL: ADL only considers the enclosing namespace of a class type. Any functions that would be found by ADL for a boost class type must be somewhere within the boost namespace.

You will be able to resolve all entities simply by doing ::std_entity for standard library entities and boost::boost_entity for boost entities.

However, I can't think of a good way to prove this without explicitly trying it out. Of course, the sure way to avoid any problems is to not do using namespace std; and, if you really have to (which you don't), only do it after all inclusions.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324