0

I know that we should not add a code like this in a header file:

using namespace std;

but what about this:

namespace foo
{
    class A
    {
     public:
        void add()
        {
             using namespace std;
             // do some work here 
         }
    }
}

As far as I know then std is only brought out into function add and no to any other classes in the name space.

Is it still not acceptable? Why?

8
  • No, and the compiler would tell you so. In any case, if class A is in namespace foo, it automatically looks up names in foo. Feb 10, 2017 at 11:42
  • You can add it to an individual member function, but not to the entire class.
    – Bo Persson
    Feb 10, 2017 at 11:52
  • @BoPersson : is it acceptable to do so? adding to individual member functions?
    – mans
    Feb 10, 2017 at 12:05
  • Why don't you ask that question, instead of asking whether code that doesn't compile is acceptable? Feb 10, 2017 at 12:07
  • @mans - I rarely do it, but it is allowed. You should also consider if typing 20 characters in the directive actually saves you anything. std:: is just 5 characters.
    – Bo Persson
    Feb 10, 2017 at 12:08

0

Your Answer

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

Browse other questions tagged or ask your own question.