3

I want using namespace std; to apply to classes as well as functions without polluting the global namespace, but I'm wondering if it's an ok approach.

namespace
{
    using namespace std;
    class S
    {
    public:
        S()
        {
            cout << "ok";
        }

        friend ostream operator<<(ostream& os, const S& s);
    };
}

Any caveats to this?

  • 1
    So long as it's in your own source file (NOT header) and any other coders working on the file don't mind, it's ok. – Neil Kirk Nov 05 '14 at 15:51

5 Answers5

1

It will work but keep in mind the following points:

  • You should limit its use in a source file, not in a header file (in general you should refrain from using unnamed namespaces in headers since they can easily mess around with your symbol definitions, especially if there are inline functions that use something from the anonymous namespace).

  • It's a bad practice and adding an additional naming hierarchy layer (i.e. the anonymous namespace) just for laziness is as bad as it sounds.

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

If it's in header file then it's not preferable as this file could be included in multiple source file. If in some source file then its acceptable

ravi
  • 10,994
  • 1
  • 18
  • 36
0

Whilst this looked like a great solution, in my experiments it doesn't do as I expected it would. It doesn't limit the scope of the using directive.

Consider this:

#include <string>

namespace {

string s1; // compile error

}

namespace {

string s2; // compile error

}

string s3; // compile error

int main()
{
}

None of those strings compile because they are not properly qualified. This is what we expect.

Then consider this:

#include <string>

namespace {

using namespace std;

string s1; // compiles fine (as expected)

}

namespace {

string t2; // compiles fine (I didn't expect that)

}

string v3; // compiles fine (I didn't expect that either)

int main()
{
}

So placing a using directive within an unnamed namespace appears to be exactly the same as placing it in the global namespace.

EDIT: Actually, placing a symbol in an unnamed namespace makes it local to the translation unit. So this is why it can't work for the intended purpose.

Therefore it has to be a no-no for headers.

Galik
  • 47,303
  • 4
  • 80
  • 117
0

An unnamed namespace doesn't contain the effects of a using-directive:

namespace A {int i;}
namespace {
  using namespace A;
}

int j=i,k=::i;        // OK

Both qualified and unqualified lookup follow the implicit using-directive for the unnamed namespace and then follow the explicit one within it. Wrapping the unnamed namespace inside another namespace limits the unqualified case to other code within that outer namespace, of course:

namespace A {int i;}
namespace B {
  namespace {
    using namespace A;
  }
  int j=i;              // OK, as above
}

int j=i,                // error: i not found
    k=B::i;             // OK

However, in either case you might as well write the using-directive outside the unnamed namespace (or not write it at all if it would be problematic, perhaps because it would appear in a header file).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
-1

Don't take the decision to use an anonymous namespace just so you can limit the scope of a using directive.

That being said, if an anonymous (or any other) namespace is already there and you want the advantages of the using inside it, it's fine as long as your coding standards okay it.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625