Avoiding namespace ambiguities - c++

This is a discussion on Avoiding namespace ambiguities - c++ ; I'm in a situation where I'd like to create a new namespace which is identical to an old one except that one type is different: namespace Original { class A; class B; // ... } namespace AlmostOriginal { using namespace ...

+ Reply to Thread
Results 1 to 3 of 3

Avoiding namespace ambiguities

  1. Default Avoiding namespace ambiguities

    I'm in a situation where I'd like to create a new namespace which is
    identical to an old one except that one type is different:

    namespace Original
    {
    class A;
    class B;
    // ...
    }

    namespace AlmostOriginal
    {
    using namespace Original;
    class B;
    }

    The above works fine as long as the contents of AlmostOriginal are used with
    qualification (AlmostOriginal::A, AlmostOriginal::B). However, if someone
    writes "using namespace AlmostOriginal" and uses unqualified B, an ambiguity
    results (since in that case both AlmostOriginal::B and Original::B are
    visible).

    Is there a solution? Any tricks which would allow unqualified B to
    unambiguously refer to AlmostOriginal::B when a using namespace
    AlmostOriginal is used?


    --
    ------------- Matti Rintala ------------ matti.rintala@tut.fi ------------
    Painting is the art of inclusion. Photography is an art of exclusion.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  2. Default Re: Avoiding namespace ambiguities

    Matti Rintala wrote:

    > I'm in a situation where I'd like to create a new namespace which is
    > identical to an old one except that one type is different:
    >
    > namespace Original
    > {
    > class A;
    > class B;
    > // ...
    > }
    >
    > namespace AlmostOriginal
    > {
    > using namespace Original;
    > class B;
    > }
    >
    > The above works fine as long as the contents of AlmostOriginal are
    > used with qualification (AlmostOriginal::A, AlmostOriginal::B).
    > However, if someone writes "using namespace AlmostOriginal" and uses
    > unqualified B, an ambiguity results (since in that case both
    > AlmostOriginal::B and Original::B are visible).
    >
    > Is there a solution? Any tricks which would allow unqualified B to
    > unambiguously refer to AlmostOriginal::B when a using namespace
    > AlmostOriginal is used?


    There is a solution, but it is not going to be nice.

    namespace AlmostOriginal
    {
    using Original::A;
    // list every name from Original in a using declaration,
    // *except* B.

    class B;
    }

    As I said, it is not nice, but it will work.
    Unfortunately, there is no easy syntax to get access to everything from
    a namespace except the parts you list. It is either everything or only
    the explicitly mentioned things.

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  3. Default Re: Avoiding namespace ambiguities

    { extraneous linebreaks removed. -mod }

    On Thu, 23 Aug 2007 10:30:46 CST, Matti Rintala
    <matti.rintala@tut.fi> wrote:

    >I'm in a situation where I'd like to create a new namespace which is
    >identical to an old one except that one type is different:
    >
    >namespace Original
    >{
    > class A;
    > class B;
    > // ...
    >}
    >
    >namespace AlmostOriginal
    >{
    > using namespace Original;
    > class B;
    >}
    >
    >The above works fine as long as the contents of AlmostOriginal are used

    with
    >qualification (AlmostOriginal::A, AlmostOriginal::B). However, if

    someone
    >writes "using namespace AlmostOriginal" and uses unqualified B, an

    ambiguity
    >results (since in that case both AlmostOriginal::B and Original::B are
    >visible).
    >
    >Is there a solution? Any tricks which would allow unqualified B to
    >unambiguously refer to AlmostOriginal::B when a using namespace
    >AlmostOriginal is used?


    The "solution", if you want to call it that, is to make sure that
    none of the header files in your project pollutes the global
    namespace (by putting extraneous "using" directives in header files).

    But I know that it is very difficult to prevent, especially with
    legacy code. Even one particular compiler, very popular on Windows --
    not Microsoft -- was guilty of this (maybe still is...can you guess
    which one? <g>)

    --
    Bob Hairgrove
    NoSpamPlease@Home.com


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 10-22-2007, 06:07 PM
  2. Avoiding ambiguities
    By Application Development in forum c++
    Replies: 2
    Last Post: 08-01-2007, 01:06 PM
  3. Oracle grammar ambiguities and conflicts
    By Application Development in forum Compilers
    Replies: 10
    Last Post: 01-20-2006, 04:13 PM
  4. Oracle grammar ambiguities and conflicts
    By Application Development in forum Compilers
    Replies: 1
    Last Post: 01-17-2006, 09:41 PM
  5. Ambiguities from Special-Case Productions
    By Application Development in forum Compilers
    Replies: 4
    Last Post: 09-22-2005, 10:50 PM