Topic: is this legal?


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 19 Apr 2002 17:08:54 GMT
Raw View
James Kanze wrote:
>
> Martin.Aupperle@Quasar-Gmbh.de (Martin Aupperle) writes:
....
> |>  One of the properties of a namespace is that it can be closed and
> |>  continued.
>
> It is a fundamental property of a namespace that it cannot be closed.
> There is no way (except by comments) that this is it, and that no more
> new members can be added.

He didn't mean permanantly closed, but temporarily closed, like the door
to a house. He was referring to the fact that you can issue a
terminating "}", and thereby resume defining symbols that are NOT part
of the namespace.

---
[ 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: James Kanze <kanze@gabi-soft.de>
Date: Thu, 18 Apr 2002 16:20:07 GMT
Raw View
Martin.Aupperle@Quasar-Gmbh.de (Martin Aupperle) writes:

|>  On Tue, 16 Apr 2002 01:49:07 GMT, "Jack J. Woehr"
|>  <jwoehr@attglobal.net> wrote:

|>  >Comeau, betcha! Makes more sense. Why should a using directive have
|>  >scope outside the curlies in which it is found?

|>  One of the properties of a namespace is that it can be closed and
|>  continued.

It is a fundamental property of a namespace that it cannot be closed.
There is no way (except by comments) that this is it, and that no more
new members can be added.

|>  It is legal to say

|>   namespace N
|>   {
|>     void f();
|>     f();  // #1
|>   }

|>   namespace N
|>   {
|>     f(); // #2
|>   }

|>  OK, #1 is clear anyway, but #2 should be also.

If they are both in the same translation unit, I would hope so.

--=20
James Kanze                                mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                    Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481

---
[ 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: Martin.Aupperle@Quasar-Gmbh.de (Martin Aupperle)
Date: Fri, 19 Apr 2002 12:29:57 GMT
Raw View
On Thu, 18 Apr 2002 16:20:07 GMT, James Kanze <kanze@gabi-soft.de>
wrote:

>Martin.Aupperle@Quasar-Gmbh.de (Martin Aupperle) writes:
>
>|>  On Tue, 16 Apr 2002 01:49:07 GMT, "Jack J. Woehr"
>|>  <jwoehr@attglobal.net> wrote:
>
>|>  >Comeau, betcha! Makes more sense. Why should a using directive have
>|>  >scope outside the curlies in which it is found?
>
>|>  One of the properties of a namespace is that it can be closed and
>|>  continued.
>
>It is a fundamental property of a namespace that it cannot be closed.
>There is no way (except by comments) that this is it, and that no more
>new members can be added.

That's splitting hairs, eh? English is not my native language. I think
the intent of my  statement (as answer to Mr. Woehr's wrong conception
of namespaces) is pretty clear if you look at the code.

Anyway, the problem obviously was that I got the two "invocations" of
f wrong. If we had Implicit Int, it were declarations and the snippet
should compile. But we don't have Implicit Int and the snippet is
ill-formed. The error message Comeau spit out simply was not
understood correctly by me.



------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ 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: Graeme Prentice <gp1@paradise.net.nz>
Date: Fri, 19 Apr 2002 09:31:27 CST
Raw View
On Fri, 19 Apr 2002 12:29:57 GMT, Martin.Aupperle@Quasar-Gmbh.de
(Martin Aupperle) wrote:

>
>Anyway, the problem obviously was that I got the two "invocations" of
>f wrong. If we had Implicit Int, it were declarations and the snippet
>should compile. But we don't have Implicit Int and the snippet is
>ill-formed. The error message Comeau spit out simply was not
>understood correctly by me.

Here's the original code again


 namespace N1
 {
   void f();     // #3
 }

 namespace N2
 {
   using namespace N1;
   f(); // #1 OK
 }

 namespace N2
 {
   f();  // #2 ???
 }

Lines #1,2,3 are all function declarations because they are at
namespace scope not block scope.  Comeau gives a correct warning about
"omission of return type is non standard  - int assumed."

As per the example in section 3.3.5 of the standard, duplicate
function declarations are legal.  Lines #1,#2 are duplicate function
declarations.  The f() in line #3 refers to a different function to
the f() at lines #1,2 because its a member of a different namespace.

If a member function of namespace N2 calls f() (unqualified) then it
resolves to the f() defined in N2

However for this code

using namespace N2;
int main()
{
    f();
    return 0;
}

an ambiguity occurs because section 7.3.4 para 5 says that a
transitive search occurs  - i.e. for the call to f() in main,
namespace N2 is searched (because of the using namespace N2) but
namespace N1 is also searched (because N2 contains the using
directive).

To avoid the anbiguity you can specify N2::f() which selects the f()
defined in N2   -   this is because of section 3.4.3.2 para 2 which
says that a transitive search for f() does occur, however, any using
directives in a namespace containing a direct declaration of f() are
ignored  - hence for N2::f(), the using namespace N1 is ignored
because N2 contains a direct declaration of f().  If namespace N2 had
no member f(), then N1 would be searched.

If a member function of namespace N2 calls f() unqualified, then no
ambiguity occurs (unlike an unqualified call to f() in main) - even
though namespace N2 contains the using namespace N1 directive.  The
reason for this is that for unqualified lookup, section 3.4.1 para 1
states that name lookup ends as soon as a declaration is found for the
name   - so for a member function of N2 calling f() unqualified, it
finds the f() member of N2 and doesn't look any further.

So, why the ambiguity for the unqualified call to f() in main?  The
reason is as specified in section 7.3.4 (using directive) paragraphs 1
and 2  - para 2 says -quote

"The using-directive is transitive: if a scope contains a
using-directive that nominates a second namespace that itself contains
using-directives, the effect is as if the using-directives from the
second namespace also appeared in the first.

so its as if you had

using namespace N1;
using namespace N2;
int main()
{
    f();
    return 0;
}

and section 7.3.4 para 1 says that for unqualified name lookup, the
names that are members of the namespace specified in the using
directive appear as if they were declared in the nearest enclosing
namespace which contains both the using-directive and the nominated
namespace.  So in the above case, the members of N1 N2 appear as if
they were declared in the global namespace.

Graeme

---
[ 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: "Jack J. Woehr" <jwoehr@attglobal.net>
Date: Tue, 16 Apr 2002 01:49:07 GMT
Raw View
Martin Aupperle wrote:

> I have written something like
>
>         namespace N1
>         {
>           void f();
>         }
>
>         namespace N2
>         {
>           using namespace N1;
>           f();  // #1 OK
>         }
>
>         namespace N2
>         {
>           f();  // #2 ???
>         }
>
> Now, is the call to f at #2 legal? MSVC thinks it is, but Comeau does
> not. Comeau wants another using-directive when reopening namespace N2.
>
> Who is correct?
>

Comeau, betcha! Makes more sense. Why should a using directive have scope
outside the curlies in which it is found? Why should it magically adhere to the
declaration namespace?

If I'm right, what do I win? :-)

--
Jack J. Woehr                 # Ceterum censeo
PO Box 51, Golden, CO 80402   # in herbas belli
http://www.softwoehr.com      # ab idem desistamus.



---
[ 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: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Tue, 16 Apr 2002 01:49:32 GMT
Raw View
On Mon, 15 Apr 2002 01:16:56 GMT, "Giovanni Bajo"
<giovannibajo@REMOVEliberoTHIS.it> wrote:

[SNIP]
>your example is ill-formed because the commented line are interpreted
>as function declaration, not function calls.
>

Without a type for the return value, unless you have
  class f {
      f(); // default constructor
  };
then:
    f();
is indeed a function call.
    void f();
is a declaration.

Am I wrong?


Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.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: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 16 Apr 2002 18:06:18 GMT
Raw View
"Jack J. Woehr" wrote:
>
> Martin Aupperle wrote:
>
> > I have written something like
> >
> >         namespace N1
> >         {
> >           void f();
> >         }
> >
> >         namespace N2
> >         {
> >           using namespace N1;
> >           f();  // #1 OK
> >         }
> >
> >         namespace N2
> >         {
> >           f();  // #2 ???
> >         }
> >
> > Now, is the call to f at #2 legal? MSVC thinks it is, but Comeau does
> > not. Comeau wants another using-directive when reopening namespace N2.
> >
> > Who is correct?
> >
>
> Comeau, betcha! Makes more sense. Why should a using directive have scope
> outside the curlies in which it is found? Why should it magically adhere to the
> declaration namespace?

Because those curlies are part of a namespace definition. That
using-directive has namespace scope. Therefore, it applies throughout
the rest of the namespace after the point where it appears. If it
appeared in a block, it would have block scope, but this isn't a block.
Those are the only two kinds of scopes permitted for a using-directive.

---
[ 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: Martin.Aupperle@Quasar-Gmbh.de (Martin Aupperle)
Date: Wed, 17 Apr 2002 17:05:21 GMT
Raw View
On Tue, 16 Apr 2002 01:49:07 GMT, "Jack J. Woehr"
<jwoehr@attglobal.net> wrote:

>
>Comeau, betcha! Makes more sense. Why should a using directive have scope
>outside the curlies in which it is found?
>
One of the properties of a namespace is that it can be closed and
continued. It is legal to say

 namespace N
 {
   void f();
   f();  // #1
 }

 namespace N
 {
   f(); // #2
 }

OK, #1 is clear anyway, but #2 should be also. It seems that Comeaus's
implementation of using-Declaration is wrong? (hard to believe).


Martin


------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ 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: Martin.Aupperle@quasar-gmbh.de (Martin Aupperle)
Date: Sun, 14 Apr 2002 15:34:59 GMT
Raw View
I have written something like

 namespace N1
 {
   void f();
 }

 namespace N2
 {
   using namespace N1;
   f(); // #1 OK
 }

 namespace N2
 {
   f();  // #2 ???
 }

Now, is the call to f at #2 legal? MSVC thinks it is, but Comeau does
not. Comeau wants another using-directive when reopening namespace N2.


Who is correct?

Martin
------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ 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: "Giovanni Bajo" <giovannibajo@REMOVEliberoTHIS.it>
Date: Mon, 15 Apr 2002 01:16:56 GMT
Raw View
"Martin Aupperle" <Martin.Aupperle@quasar-gmbh.de> ha scritto nel messaggio
news:3cb994f8.13879637@News.CIS.DFN.De...
> I have written something like
>
> namespace N1
> {
>   void f();
> }
>
> namespace N2
> {
>   using namespace N1;
>   f(); // #1 OK
> }
>
> namespace N2
> {
>   f(); // #2 ???
> }
>
> Now, is the call to f at #2 legal? MSVC thinks it is, but Comeau does
> not. Comeau wants another using-directive when reopening namespace N2.

Which version? My Comeau works happily with it. Anyway, your example is
ill-formed because the commented line are interpreted as function
declaration, not function calls.

Giovanni Bajo

---
[ 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                       ]