Anonymous Namespaces With Using Directives

Hello there!

Essentially what I want to know is if it's possible to use an anonymous namespace to somehow nest a using directive without it being applied to the global scope.

Essentially I have something that looks like this:

1
2
3
4
5
6
7
8
namespace {

	using std::string;
	using std::map;
	using std::function;

	class lTest
	{


For example, the using std::string also applies to the global scope, so I can use "string" instead of "std::string" outside the scope of the namespace. I don't really want that, is there any way to avoid that? Or is there any other way for me to accomplish this in a simple way?

I want to write a class that's accessible globally, that doesn't require me to specify MyNameSpace::MyClass, and I want to be able to use std::string, std::function, and whatnot only in my class so my code looks like this:

1
2
3
4
5
const map< string, function< void() > > myThings =
{
	{ "x", [ this ](){ printX(); } },
	{ "y", [ this ](){ printY(); } }
};


Instead of this:

1
2
3
4
5
const std::map< std::string, std::function< void() > > myThings =
{
	{ "x", [ this ](){ printX(); } },
	{ "y", [ this ](){ printY(); } }
};


Any way to do this without polluting the global scope?
Last edited on
(At first, I didn't catch that you needed an anonymous namespace.)

Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace {
  namespace internal
  {
    using std::string;

    struct myclass
    {
      string s;
    };
  }

  using internal::myclass;
}
Topic archived. No new replies allowed.