3

I am writing a small library. Declaration of my classes, functions and others, that uses standard library are in a header file. I know that putting "using namespace" into header is a bad practic. May I put my code in separate namespace and then put "using namespace" into it? Like this:

// header.h
namespace My
{
    using namespace std;
    // declarations
}

Will it be good?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Dmitry
  • 47
  • 4
  • you can, and it will bring the things from that namespace into yours, but it is generally bad to use `using namespace` anywhere. – Dave Dec 01 '13 at 14:02
  • 2
    Never put `using namespace std` anywhere! – sgarizvi Dec 01 '13 at 14:02
  • Regarding the specific `using namespace std` [here's a detailed post on why not to do that](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Chris O Dec 01 '13 at 14:12

2 Answers2

3

Don't do it!
Simply use fully qualified names or using declaration for specific symbols that you want to use.
With this, You will just end up importing the contents of entire std namespace in your namespace My and essentially the header file header.h. Basically, it is going to give you namespace pollution with lot of unused symbols and also increase the compilation time of every translation unit where you include this header.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Is better putting any using declaration into namespace than putting it directly in the header file, or not? – Dmitry Dec 01 '13 at 14:31
  • @Dmitry: You are asking to chose between two evils.Yes, your method is less evil but doesn't mean its not evil. – Alok Save Dec 01 '13 at 14:44
1

You may do that but it is not a good idea because this can lead to ambiguious names.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335