Topic: Namespace and class names in the same scope


Author: no.spam@no.spam.com (Maciej Sobczak)
Date: Fri, 2 Feb 2007 14:33:17 GMT
Raw View
Consider the following:

// 1.cpp
namespace A {}
class A {};

// 2.cpp
namespace A {}
class A{};
A a;

// 3.cpp
namespace A {}
namespace B
{
      class A {};
}
using B::A;

// 4.cpp
namespace B
{
      class A {};
}
using B::A;
namespace A {}

// 5.cpp
namespace B
{
      class A {};
}
using B::A;
A a;
namespace A {}

// 6.cpp
namespace B
{
      class A {};
}
using B::A;
namespace A {}
A a;


What interst me most is the fact that 3.cpp does not seem to be legal,
even though 1.cpp is fine. Why?

In any case, I would appreciate a sentence of explanation for each of
the above (some of them are legal).

--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: greghe@pacbell.net (Greg Herlihy)
Date: Sun, 4 Feb 2007 06:05:26 GMT
Raw View
On 2/2/07 6:33 AM, in article epv4er$sme$1@cernne03.cern.ch, "Maciej
Sobczak" <no.spam@no.spam.com> wrote:

> Consider the following:
>=20
> // 1.cpp
> namespace A {}
> class A {};
>=20
> // 2.cpp
> namespace A {}
> class A{};
> A a;
>=20
> // 3.cpp
> namespace A {}
> namespace B
> {
>       class A {};
> }
> using B::A;
>=20
> // 4.cpp
> namespace B
> {
>       class A {};
> }
> using B::A;
> namespace A {}
>=20
> // 5.cpp
> namespace B
> {
>       class A {};
> }
> using B::A;
> A a;
> namespace A {}
>=20
> // 6.cpp
> namespace B
> {
>       class A {};
> }
> using B::A;
> namespace A {}
> A a;
>=20
>=20
> What interst me most is the fact that 3.cpp does not seem to be legal,
> even though 1.cpp is fine. Why?

Each of these examples is ill-formed and each one for the same reason:
namely that the namespace "A" has the same name as another entity either =
-
declared in (class A) or introduced into (class B::A) - the same declarat=
ive
region.=20

The exact rule is:

"A namespace-name or namespace-alias shall not be declared as the name of
any other entity in the same declarative region. A namespace-name defined=
 at
global scope shall not be declared as the name of any other entity in any
global scope of the program." [=A77.3.2/4]

Greg

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]