using directive being transitive

Discussion in 'C++' started by Imre, Aug 17, 2005.

  1. Imre

    Imre Guest

    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
    Imre, Aug 17, 2005
    #1
    1. Advertising

  2. 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::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
    Victor Bazarov, Aug 17, 2005
    #2
    1. Advertising

  3. Imre

    Imre Guest

    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::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;


    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
    Imre, Aug 18, 2005
    #3
  4. Imre

    Imre Guest

    Imre, Aug 18, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Shemitz

    Transitive references

    Jon Shemitz, Feb 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    578
    Jon Shemitz
    Feb 5, 2004
  2. Insert Pseudonym Here

    using-declaration vs. using-directive

    Insert Pseudonym Here, May 3, 2004, in forum: C++
    Replies:
    1
    Views:
    1,715
    Rob Williscroft
    May 3, 2004
  3. Replies:
    11
    Views:
    812
    Tomás Ó hÉilidhe
    Dec 19, 2007
  4. Niels Dekker - no reply address

    Using-declaration or using-directive inside unnamed-namespace?

    Niels Dekker - no reply address, Apr 27, 2010, in forum: C++
    Replies:
    1
    Views:
    575
    Niels Dekker - no reply address
    Apr 27, 2010
  5. Francois Grieu

    is the < operator transitive?

    Francois Grieu, Oct 7, 2011, in forum: C Programming
    Replies:
    52
    Views:
    1,090
    Tim Rentsch
    Jan 25, 2012
Loading...

Share This Page