Topic: Can a using-declaration name a namespace?


Author: Victor Bazarov <v.Abazarov@comAcast.net>
Date: 16 Jul 2004 23:25:05 GMT
Raw View
Gabriel Dos Reis wrote:
> stephen.clamage@sun.com (Steve Clamage) writes:
>
> | Boris Kolpackov wrote:
> | > Good day,
> | > There is a defect report #406
> | > http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#460
> | > that questions whether the following is legal or not (which is
> | > legal according to the standard):
> | > namespace n
> | > {
> | >   namespace m
> | >   {
> | >   }
> | > }
> | > using n::m;
> | > While the defect report does not say what exactly the defect is,
> | > it has the following note:
> | > "Notes from the March 2004 meeting:
> | > We agree that it should be an error."
> | > Can somebody explain to the rest of us why exactly it should be an
> | > error?
> | > thanks,
> | > -boris
> |
> | Here's my take on the issue.
> |
> | To get access to names in namespace foo, you write a using-directive:
> |  using namespace foo;
> | not a using-declaration:
> |  using foo;
>
> Yes but that is not what Boris presented.
>
> As you recalled in your message, a using-directive does not have the
> same effect as a using-declaration.  A using-declaration selectively
> brings into scope a specific set of declarations.  Using-directives on
> the other hand profondly modifies the way look-up is done.  They are
> quite different things and I believe that reducing the issue to that
> of using-directives is not right.
>
> The inconsistency, as I understand it from Boris's message, is:
>
>    namespace N
>    {
>        struct S { };
>
>        namespace M { }
>    }
>
>
>    using N::S;          // OK
>    using N::M;          // would be ERROR according to the #406
>
> I must confess I'm confused as to why it should be an error.  As far
> as I can tell, the effect is to bring the name "M" into scope, not to
> dump the declarations in N::M:: into the current scope.
>
> I believe that the resolution of #406 is defective.
>
> | A using-declaration introduces the *name* into the current namespace.
> | It is not the same as a using-directive.
>
> Yes, agreed.  But Boris does not want using-directive.  He wants to
> introduces the *name* "m" into the current scope.  I see no technical
> reason to forbid that.

I believe the name "m" needs classification when it's injected.  And
that classification is "either it's a namespace or it isn't".  Just
"adding a name" won't work because names of namespaces are treated
differently from names of types and names of objects.

>
> | So in your example, the name "m" from namespace "n" would be
> | introduced into the global namespace. But "m" is the name of a
> | namespace, so introducing the name doesn't have any useful effect.
>
> Why?

Because it won't.

>
> It has the effect that he would just say "m" witthout:
>
>   (1) the qualification
>   (2) without dumping the whole content into the current scope.

There is already a mechanism in the language to do that.  It's called
"namespace aliasing".

>
>
> | I wasn't present for the discussion of this issue, but I suppose the
> | idea was to make
> |  using <namespace-name> ;
> | an error, since it probably is a programmer mistake anyway.
>
> That might be the idea but I don't find it particularly compelling.
> Why would it be likely an error to say
>
>    using <namespace-name> ;
>
> and
>
>    using <class-name> ;
>
> would not?
>
> I would suggest we trust the programmer and have, simple, general rules.

A simple, general rule exists.  It basically makes any namespace name
_special_.  The name, when declared, is not added to the list of "all
known names", but gets put in a "all known namespaces".  So, declaring
a namespace (to introduce its particular name into the set of namespaces
for, say, searching for a name when resolving a qualified name) is
already there:

     namespace nm = n::m;

.

Correct me if I am wrong.

Victor

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: boris@kolpackov.net (Boris Kolpackov)
Date: Sat, 17 Jul 2004 02:42:28 +0000 (UTC)
Raw View
stephen.clamage@sun.com (Steve Clamage) writes:

> So in your example, the name "m" from namespace "n" would be
> introduced into the global namespace. But "m" is the name of a
> namespace, so introducing the name doesn't have any useful effect.

It is as useful as any other using-declaration -- it brings mentioned
name into a scope which allows me to refer to such a name without any
further qualification:

namespace n
{
  namespace m
  {
    void f ();
  }
}


void g ()
{
  n::m::f ();

  using n::m;

  // Now `m' is in the scope so I can use it unqualified.

  m::f ();
}

Hope it is clear now.

-boris

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ux9i003@yahoo.com (Max Polk)
Date: Sat, 17 Jul 2004 05:05:33 +0000 (UTC)
Raw View
Gabriel Dos Reis wrote:
>    namespace N
>    {
>        struct S { };
>
>        namespace M { }
>    }
>
>    using N::S;          // OK
>    using N::M;          // would be ERROR according to the #406
>
> I must confess I'm confused as to why it should be an error.  As far
> as I can tell, the effect is to bring the name "M" into scope, not to
> dump the declarations in N::M:: into the current scope.
...
> It has the effect that he would just say "m" witthout:
>
>   (1) the qualification
>   (2) without dumping the whole content into the current scope.

Essentially you want to have the effect of saying M::Z instead of N::M::Z
[where Z is in the M namespace], but without bringing S and everything else in
N into the current scope.

This actually presents a problem if there is another namespace M adjacent to
N, as in:

     namespace N
     {
         struct S { };
         namespace M
         {
             struct Z { };
         }
     }
     namespace M
     {
         struct Z { };
     }

     M::Z a;             // this is not N::M::Z
     using N::M;         // your suggestion
     M::Z b;             // ambiguous

If you *were* able to bring the N::M namespace into current scope, M::Z would
then be ambiguous, and it would be *impossible* to refer to the Z inside M
which is in turn not inside N.

You might wish to do this:

     ::M::Z b;           // solved ambiguity?

But you would be preventing somebody from wrapping your header file with all
these definitions inside a namespace to keep it separate from their code:

     namespace VENDOR
     {
     #include "filename.h"
     }

You haven't solved the ambiguity because ::M::Z isn't right, it's really
prefixed by VENDOR by whoever is including your code.

Basically you raise an ambiguity impossible to solve.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gdr@cs.tamu.edu (Gabriel Dos Reis)
Date: Sat, 17 Jul 2004 07:16:40 +0000 (UTC)
Raw View
ux9i003@yahoo.com (Max Polk) writes:

[...]

| This actually presents a problem if there is another namespace M
| adjacent to N, as in:

That is a non-argument.  See below.

|      namespace N
|      {
|          struct S { };
|          namespace M
|          {
|              struct Z { };
|          }
|      }
|      namespace M
|      {
|          struct Z { };
|      }
|
|      M::Z a;             // this is not N::M::Z
|      using N::M;         // your suggestion

That particular configuration would be invalid in similar ways as

    namespace N {
       struct S { };
    };

    struct S { };
    using N::S;

by general rules.

[...]

| Basically you raise an ambiguity impossible to solve.

No.  See above.

--
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
  Texas A&M University -- Computer Science Department
 301, Bright Building -- College Station, TX 77843-3112

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: musiphil@bawi.org (Seungbeom Kim)
Date: Sun, 18 Jul 2004 00:10:25 +0000 (UTC)
Raw View
Max Polk wrote:
>
> This actually presents a problem if there is another namespace M
> adjacent to N, as in:
>
>     namespace N
>     {
>         struct S { };
>         namespace M
>         {
>             struct Z { };
>         }
>     }
>     namespace M
>     {
>         struct Z { };
>     }
>
>     M::Z a;             // this is not N::M::Z
>     using N::M;         // your suggestion
>     M::Z b;             // ambiguous
>
> [...]
> Basically you raise an ambiguity impossible to solve.

Is the situation different in any way with M being a name of a type or
an object instead of a namespace? I don't think so. If the possibility
of ambiguity were a problem that prevented us from using-declarations,
we wouldn't be able to use any using-declaration at all, would we?

--
Seungbeom Kim

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: bop@gmb.dk ("Bo Persson")
Date: Sun, 18 Jul 2004 00:12:57 +0000 (UTC)
Raw View
"Boris Kolpackov" <boris@kolpackov.net> skrev i meddelandet
news:2lqa97FfrhqbU1@uni-berlin.de...
> stephen.clamage@sun.com (Steve Clamage) writes:
>
> > So in your example, the name "m" from namespace "n" would be
> > introduced into the global namespace. But "m" is the name of a
> > namespace, so introducing the name doesn't have any useful effect.
>
> It is as useful as any other using-declaration -- it brings mentioned
> name into a scope which allows me to refer to such a name without any
> further qualification:
>
> namespace n
> {
>   namespace m
>   {
>     void f ();
>   }
> }
>
>
> void g ()
> {
>   n::m::f ();
>
>   using n::m;

But this feature already exists, you just write it:

namespace m = n::m;

>
>   // Now `m' is in the scope so I can use it unqualified.

Right!

>
>   m::f ();
> }
>


Bo Persson



---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gdr@cs.tamu.edu (Gabriel Dos Reis)
Date: Sun, 18 Jul 2004 03:50:02 +0000 (UTC)
Raw View
bop@gmb.dk ("Bo Persson") writes:

[...]

| > namespace n
| > {
| >   namespace m
| >   {
| >     void f ();
| >   }
| > }
| >
| >
| > void g ()
| > {
| >   n::m::f ();
| >
| >   using n::m;
|
| But this feature already exists,

No, it does not.

| you just write it:
|
| namespace m = n::m;

Then the same reasoning implies that using-declaration that brings in
type-names should be abolished because we have typedef.

   namespace N {
     struct S { };
   };

   using N::S;        // should be rejected, because it could be
                      // written typedef N::S S;

--
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
  Texas A&M University -- Computer Science Department
 301, Bright Building -- College Station, TX 77843-3112

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gdr@cs.tamu.edu (Gabriel Dos Reis)
Date: Tue, 20 Jul 2004 18:14:30 GMT
Raw View
Victor Bazarov <v.Abazarov@comAcast.net> writes:

[...]

| > The inconsistency, as I understand it from Boris's message, is:
| >    namespace N
| >    {
| >        struct S { };
| >        namespace M { }
| >    }
| >       using N::S;          // OK
| >    using N::M;          // would be ERROR according to the #406
| > I must confess I'm confused as to why it should be an error.  As far
| > as I can tell, the effect is to bring the name "M" into scope, not to
| > dump the declarations in N::M:: into the current scope.
| > I believe that the resolution of #406 is defective.
| > | A using-declaration introduces the *name* into the current
| > namespace.
| > | It is not the same as a using-directive.
| > Yes, agreed.  But Boris does not want using-directive.  He wants to
| > introduces the *name* "m" into the current scope.  I see no technical
| > reason to forbid that.
|
| I believe the name "m" needs classification when it's injected.  And

When the name is brought into scope through a using-declaration, it is
not forgetfully done so.  That is, by the time the using-declaration
was processed, the compiler looked into the scope named by the
nested-qualifier and determined that effectively "m" is a namespace.
When it bring that name into scope, it annotates it (someway of the
other) as to what it names.  That, irrespectively of whether that name
designate a type-name or a fubhar.  So the objection you present is not
a valid one.

| that classification is "either it's a namespace or it isn't".  Just
| "adding a name" won't work because names of namespaces are treated
| differently from names of types and names of objects.

Yes, that but a using-declatation (whether it brings a namespace-name
or a fubhar-name into scope) does not forget about what that name
happens to designate in reality.  Nobody is asking that "m" to be
introduced as a symbol in void.

| > | So in your example, the name "m" from namespace "n" would be
| > | introduced into the global namespace. But "m" is the name of a
| > | namespace, so introducing the name doesn't have any useful effect.
| > Why?
|
| Because it won't.

That is untrue, for at least the two useful effects I listed below

| > It has the effect that he would just say "m" witthout:
| >   (1) the qualification
| >   (2) without dumping the whole content into the current scope.
|
| There is already a mechanism in the language to do that.  It's called
| "namespace aliasing".

So what?  Following your logic a using-declaration that names a type
should be banned because we already have a mechanism in the language
to do that, namely typedef.  I believe that the coherence of that
reasoning does not resist any serious analyzis.

| > | I wasn't present for the discussion of this issue, but I suppose
| > the
| > | idea was to make
| > |  using <namespace-name> ;
| > | an error, since it probably is a programmer mistake anyway.
| > That might be the idea but I don't find it particularly compelling.
| > Why would it be likely an error to say
| >    using <namespace-name> ;
| > and
| >    using <class-name> ;
| > would not?
| > I would suggest we trust the programmer and have, simple, general
| > rules.
|
| A simple, general rule exists.  It basically makes any namespace name
| _special_.

No, it does not.

|  The name, when declared, is not added to the list of "all
| known names", but gets put in a "all known namespaces".

just as the same is done for class-names, typedef-names or non-type names.

| So, declaring
| a namespace (to introduce its particular name into the set of namespaces
| for, say, searching for a name when resolving a qualified name) is
| already there:
|
|      namespace nm = n::m;
|
| .
|
| Correct me if I am wrong.

Repeat the same for type-names.

--
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
  Texas A&M University -- Computer Science Department
 301, Bright Building -- College Station, TX 77843-3112

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: boris@kolpackov.net (Boris Kolpackov)
Date: Tue, 13 Jul 2004 02:50:08 +0000 (UTC)
Raw View
Good day,

There is a defect report #406

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#460

that questions whether the following is legal or not (which is
legal according to the standard):

namespace n
{
  namespace m
  {
  }
}

using n::m;

While the defect report does not say what exactly the defect is,
it has the following note:

"Notes from the March 2004 meeting:

We agree that it should be an error."

Can somebody explain to the rest of us why exactly it should be
an error?


thanks,
-boris

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sdnarwade@yahoo.com (Sandeep)
Date: Tue, 13 Jul 2004 14:04:44 +0000 (UTC)
Raw View
boris@kolpackov.net (Boris Kolpackov) wrote in message news:<2lgaksFcl1qcU1@uni-berlin.de>...
> Good day,
>
> There is a defect report #406
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#460
>
> that questions whether the following is legal or not (which is
> legal according to the standard):
>
> namespace n
> {
>   namespace m
>   {
>   }
> }
>
> using n::m;
>
> While the defect report does not say what exactly the defect is,
> it has the following note:
>
> "Notes from the March 2004 meeting:
>
> We agree that it should be an error."
>
> Can somebody explain to the rest of us why exactly it should be
> an error?
>
>
> thanks,
> -boris
>
> ---
> [ 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.jamesd.demon.co.uk/csc/faq.html                       ]

Hi,
I am novice to the C++,so if u think i am wrong then plz ignore my
comment.
The question was whether following is legal or not...
> namespace n
> {
>   namespace m
>   {
>   }
> }
>
> using n::m;

It is not legal .The problem might be the "namespace" word that we
need to use after "using" .
The legal syntax is

using namespace n::m;

It will work.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: Fri, 16 Jul 2004 01:04:14 +0000 (UTC)
Raw View
Boris Kolpackov wrote:
> Good day,
>
> There is a defect report #406
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#460
>
> that questions whether the following is legal or not (which is
> legal according to the standard):
>
> namespace n
> {
>   namespace m
>   {
>   }
> }
>
> using n::m;
>
> While the defect report does not say what exactly the defect is,
> it has the following note:
>
> "Notes from the March 2004 meeting:
>
> We agree that it should be an error."
>
> Can somebody explain to the rest of us why exactly it should be
> an error?
>
>
> thanks,
> -boris

Here's my take on the issue.

To get access to names in namespace foo, you write a using-directive:
 using namespace foo;
not a using-declaration:
 using foo;

A using-declaration introduces the *name* into the current namespace.
It is not the same as a using-directive.

So in your example, the name "m" from namespace "n" would be
introduced into the global namespace. But "m" is the name of a
namespace, so introducing the name doesn't have any useful effect.

I wasn't present for the discussion of this issue, but I suppose the
idea was to make
 using <namespace-name> ;
an error, since it probably is a programmer mistake anyway.

---
Steve Clamage, stephen.clamage@sun.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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gdr@cs.tamu.edu (Gabriel Dos Reis)
Date: Fri, 16 Jul 2004 03:14:50 +0000 (UTC)
Raw View
stephen.clamage@sun.com (Steve Clamage) writes:

| Boris Kolpackov wrote:
| > Good day,
| > There is a defect report #406
| > http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#460
| > that questions whether the following is legal or not (which is
| > legal according to the standard):
| > namespace n
| > {
| >   namespace m
| >   {
| >   }
| > }
| > using n::m;
| > While the defect report does not say what exactly the defect is,
| > it has the following note:
| > "Notes from the March 2004 meeting:
| > We agree that it should be an error."
| > Can somebody explain to the rest of us why exactly it should be an
| > error?
| > thanks,
| > -boris
|
| Here's my take on the issue.
|
| To get access to names in namespace foo, you write a using-directive:
|  using namespace foo;
| not a using-declaration:
|  using foo;

Yes but that is not what Boris presented.

As you recalled in your message, a using-directive does not have the
same effect as a using-declaration.  A using-declaration selectively
brings into scope a specific set of declarations.  Using-directives on
the other hand profondly modifies the way look-up is done.  They are
quite different things and I believe that reducing the issue to that
of using-directives is not right.

The inconsistency, as I understand it from Boris's message, is:

   namespace N
   {
       struct S { };

       namespace M { }
   }


   using N::S;          // OK
   using N::M;          // would be ERROR according to the #406

I must confess I'm confused as to why it should be an error.  As far
as I can tell, the effect is to bring the name "M" into scope, not to
dump the declarations in N::M:: into the current scope.

I believe that the resolution of #406 is defective.

| A using-declaration introduces the *name* into the current namespace.
| It is not the same as a using-directive.

Yes, agreed.  But Boris does not want using-directive.  He wants to
introduces the *name* "m" into the current scope.  I see no technical
reason to forbid that.

| So in your example, the name "m" from namespace "n" would be
| introduced into the global namespace. But "m" is the name of a
| namespace, so introducing the name doesn't have any useful effect.

Why?

It has the effect that he would just say "m" witthout:

  (1) the qualification
  (2) without dumping the whole content into the current scope.


| I wasn't present for the discussion of this issue, but I suppose the
| idea was to make
|  using <namespace-name> ;
| an error, since it probably is a programmer mistake anyway.

That might be the idea but I don't find it particularly compelling.
Why would it be likely an error to say

   using <namespace-name> ;

and

   using <class-name> ;

would not?

I would suggest we trust the programmer and have, simple, general rules.

--
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
  Texas A&M University -- Computer Science Department
 301, Bright Building -- College Station, TX 77843-3112

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]