Topic: namespace & global scope


Author: Rick Hollinbeck <rickh@westernwares.com>
Date: 1997/06/08
Raw View
I have a question concerning the interaction between global and
namespace symbol scopes.

This is not clear (at least to me) from the Apr 95 spec I am reading.

My question: are namespace scopes nested inside the global scope?

IOW, is x visible below?

int x;
namespace NS1 {

void foo() { int i = x; }
}

or ...

class A {};
namespace NS2 {
 class A; /* does this refer to class ::A or forward declare a new class
NS2::A? */
  }

Section 3.4.5 (class.scope) describes this in terms of all the
"enclosing namespaces". But is the global namespace an enclosing
namespace for all named and unnamed namespaces?

If not, is there a way to explicitly introduce the global names in a
namespace? e.g. "namespace NS1 { using namespace :: ; class A; }"

If so, I suppose a separate class NS2::A cannot be defined in the above
example.

Thanks for any clarification.

--

- Rick Hollinbeck (rickh@westernwares.com)
  Web: http://www.westernwares.com/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/06/09
Raw View
Rick Hollinbeck wrote:

> This is not clear (at least to me) from the Apr 95 spec I am reading.

You might want to pick up the more recent CD-2 (see the banner below,I
think).

> My question: are namespace scopes nested inside the global scope?

Yes

> IOW, is x visible below?
>
> int x;
> namespace NS1 {
>
> void foo() { int i = x; }
> }

Yes.

> or ...
>
> class A {};
> namespace NS2 {
>  class A; /* does this refer to class ::A or forward declare a new
> class
> NS2::A? */
>   }

You're introducing a new class-name in NS2.

> Section 3.4.5 (class.scope) describes this in terms of all the
> "enclosing namespaces". But is the global namespace an enclosing
> namespace for all named and unnamed namespaces?

The scope of global names extends from the point of declarationto the
end of the translation unit.

> If not, is there a way to explicitly introduce the global names in a
> namespace? e.g. "namespace NS1 { using namespace :: ; class A; }"

No such syntax (though I've talked to people who would like that sortof
feature).

> If so, I suppose a separate class NS2::A cannot be defined in the
> above
> example.

See above.

    Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]