using directive being transitive

I

Imre

As far as I know, the using directive is transitive, and I'm not sure I
like this. So I'd like to ask if there's any workaround. Consider this:

namespace MyNameSpace
{
void
F(SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace::SomeType a);
void
G(SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpaceSomeOtherType
a);
// ...
// Lots of such function declarations using types in
YetAnotherNameSpace
// ...
}

It's really inconvenient to always use these long qualifications, so it
would be nice to write using namespace
SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace; at the
beginning of MyNameSpace. However, that would mean that client code
using MyNameSpace would see everything inside YetAnotherNameSpace, and
I think this is bad. Client code should be able to decide if thay want
to use the long syntax to access names inside YetAnotherNameSpace or
write their own using directive. The using at the beginning of
MyNameSpace would be an implementation detail; it's only purpose is
making the life of the implementor of MyNameSpace easier, but it
shouldn't force clients into using any other namespaces.

So, is this, or something similar possible in C++?

Thanks,

Imre
 
V

Victor Bazarov

Imre said:
As far as I know, the using directive is transitive, and I'm not sure I
like this. So I'd like to ask if there's any workaround. Consider this:

namespace MyNameSpace
{
void
F(SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace::SomeType a);
void
G(SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpaceSomeOtherType
a);
// ...
// Lots of such function declarations using types in
YetAnotherNameSpace
// ...
}

It's really inconvenient to always use these long qualifications, so it
would be nice to write using namespace
SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace; at the
beginning of MyNameSpace.

It might be better to have a namespace declaration instead... Or maybe
have typedefs for particular types involved, like

typedef
SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace::SomeType SSYS;
> However, that would mean that client code
using MyNameSpace would see everything inside YetAnotherNameSpace, and
I think this is bad. Client code should be able to decide if thay want
to use the long syntax to access names inside YetAnotherNameSpace or
write their own using directive. The using at the beginning of
MyNameSpace would be an implementation detail; it's only purpose is
making the life of the implementor of MyNameSpace easier, but it
shouldn't force clients into using any other namespaces.

So, is this, or something similar possible in C++?

I am not really sure what you mean here, could you give an example using
C++ instead of English? It doesn't have to compile. Just comment it.

V
 
I

Imre

Victor said:
It might be better to have a namespace declaration instead... Or maybe
have typedefs for particular types involved, like

typedef
SomeNameSpace::SomeOtherNameSpace::YetAnotherNameSpace::SomeType SSYS;

This works, but can be tedious if there are many types involved.
I am not really sure what you mean here, could you give an example using
C++ instead of English? It doesn't have to compile. Just comment it.

Sure.

namespace n1
{
namespace n2
{
namespace n3
{
class T1 {};
class T2 {};
}
}
}

namespace ns
{
// just for convenience while declaring F1 and F2 (and possibly many
more such functions)
using namespace n1::n2::n3;

T1 F1();
T2 F2();

// these don't use anything from n3
void F3();
void F4();
}

// client code

class T1 {};

void F()
{
// for unqualified calls to ns::F3() and ns::F4()
// note that we don't use anything from n1::n2::n3
using namespace ns;

F3();
F4();

T1 t1; // T1 is ambiguous symbol.
}

The problem here is that even though client code is not interested in
stuff inside n1::n2::n3, it still gets them as a bonus, just because
the imlpementor of ns wanted some convenience.

What I'd like to do is change the
using namespace n1::n2::n3;
line in ns to something that says "I want to see everything in
n1::n2::n3, but anyone using me should _not_ automatically see them".
Names in n1::n2::n3 should be visible only while ns is open, not when
being used. Something like a "private using".

Imre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top