473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using directive being transitive

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::YetAnotherNam eSpace::SomeType a);
void
G(SomeNameSpace::SomeOtherNameSpace::YetAnotherNam eSpaceSomeOtherType
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::YetAnotherNameS pace; 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

Aug 17 '05 #1
3 1859
Imre wrote:
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::YetAnotherNam eSpace::SomeType a);
void
G(SomeNameSpace::SomeOtherNameSpace::YetAnotherNam eSpaceSomeOtherType
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::YetAnotherNameS pace; 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::YetAnotherNameS pace::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
Aug 17 '05 #2
Victor Bazarov wrote:
Imre wrote:
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::YetAnotherNam eSpace::SomeType a);
void
G(SomeNameSpace::SomeOtherNameSpace::YetAnotherNam eSpaceSomeOtherType
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::YetAnotherNameS pace; 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::YetAnotherNameS pace::SomeType SSYS;


This works, but can be tedious if there are many types involved.
> 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.


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

Aug 18 '05 #3

After some googling I found this:

http://www.open-std.org/jtc1/sc22/wg...1994/N0560.asc

Towards the end of the document the author discusses the same problem I
tried to explain. It seems that the current standard doesn't offer any
solutions.

Imre

Aug 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
0
by: Clifford Heath | last post by:
We've set up an SQL Server 2000 (build 8.0.761 - I think that's SP3) access control scenario like the one described in this article:...
14
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
7
by: David | last post by:
Hi there, I'm having some trouble identifying transitive dependencies in the student table below StudId Name CourseCode CourseDesc Lecturer Grade Office 'S1234', 'Jack', ...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
3
by: Wayne Shu | last post by:
When I read the chapter of the namespace of the book C++ Primer(3e). It explain the using directive as follow: "A using directive makes the namespace member names visible as if they were declared...
23
by: JDT | last post by:
Hi, It seems that using floats as the first tupelo for an STL map (shown below) can cause problems but I don't remember what the problems were. Is it common practice to use a structure like...
2
by: papinceto87 | last post by:
#!/usr/bin/env perl ################################################################ ### gsyn2 ### Plamena Dragieva ### Oct 30, 2008...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.