2

I know that a question very similar has been asked before, "using namespace" in c++ headers, but mine is when its inside of a namespace.

namespace foo {
    using namespace std; // what does this do?

    class Bar { ... }
}

Does this do exactly the same thing as the other question says and just include that using statement everywhere? Does it only do it in that namespace? Does it only do it in that header?

Community
  • 1
  • 1
David Stocking
  • 1,200
  • 15
  • 26

1 Answers1

8

There are two differences:

  1. Yes, the using will only apply to code inside the foo namespace. But that means all foo code that sees this header, so you probably still don't want to do it.
  2. If the namespace foo::std exists, the nested using statement will favor it over ::std.
dlf
  • 9,045
  • 4
  • 32
  • 58
  • 1
    Also, `using namespace foo;` will bring the whole `std` namespace into the global scope, which is not what one probably wants. – vsoftco Feb 19 '15 at 18:30
  • 1
    Also, only the parts of `std` visible before the `using namespace std;` in the current translation unit get imported into `foo`. In a different translation unit, different sets of symbols can be imported into `foo` because different headers where included. If you have any inline code in `foo`, this can cause your program to be ill-formed (no diagnostic required), where the same function has two distinct definitions in two different compilation units, or other similar problems. – Yakk - Adam Nevraumont Feb 19 '15 at 19:06
  • Thanks for the answer. Wish I found that other question that answers this before. :( Wouldn't waste others time. – David Stocking Feb 19 '15 at 20:09