namespacesc++-clitypedefusing-statement

Is it acceptable to add a "using namespace" immediately after the namespace declaration?


I have a small namespace containing some type definitions, which I use to make my code look cleaner. However I don't want to have to add a "using namespace ..." line to every file that uses one of these types, after all I already have to add a #include for the file.


MyFile.cpp:

#include "typedefs.h"
void Bob()
{
    IntList^ list = gcnew IntList;
}

typedefs.h:

namespace Typedefs
{
    typedef List<int> IntList;
    typedef array<int> IntArray;
    typedef List<Byte> ByteList;
    typedef array<Byte> ByteArray;
    typedef List<String^> StringList;
    typedef array<String^> StringArray;
}
using namespace Typedefs;

Would it be acceptable to add the "using namespace" line immediately after the namespace declaration? If not, why not?


Solution

  • Use an unnamed namespace. If you want to have the names visible only to the files in which you have included the headers

    namespace {
      int i = 10;
    }
    

    above has the same effect as like below code

    namespace randomName {
      int i = 10;
    }
    using randomName; 
    

    so nothing will be accessible from any other file.