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?

3
  • you can, and it will bring the things from that namespace into yours, but it is generally bad to use using namespace anywhere. Commented Dec 1, 2013 at 14:02
  • 2
    Never put using namespace std anywhere! Commented Dec 1, 2013 at 14:02
  • Regarding the specific using namespace std here's a detailed post on why not to do that Commented Dec 1, 2013 at 14:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is better putting any using declaration into namespace than putting it directly in the header file, or not?
@Dmitry: You are asking to chose between two evils.Yes, your method is less evil but doesn't mean its not evil.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.