Topic: using namespace


Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: Thu, 12 Apr 2001 09:42:49 GMT
Raw View
What is the scope of "using namespace"?  The following compiles
namespace Action
{
   enum Why {cause};
};
class How
{
public:
   using namespace Action;  // point 1
   int Foo(void);
};
int How::Foo(void)
{
   using namespace Action;  // point 2
   return cause;
}
int main(void)
{
   How how;
   int rc = how.Foo();
   return rc?rc:cause;  // point 3
}

With my compiler, removing the line labeled "piont 1" causes an error on the
line labeled point 3.  This implies to me that the scope of the using
statement on the line labeled "point 2" is limited in scope to the function
it is used in.  However, I would expect that the using statement on the line
labeled "point 1" to be limited in scope in the class How so that I could
remove the point 2 line although I'd still have an error on the point 3
line.  But I don't get an error on the point 3 line if the point 1 line is
in place.

Is my compiler working correctly?  If so, shouldn't the using statement in
my class be limited in scope to that class?

Greg Brewer


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Constantine S. Timoshenkov" <constim@SoftHome.net.CUT_THIS>
Date: Fri, 13 Apr 2001 02:13:23 GMT
Raw View
Greg Brewer wrote in message <9b1uim$19su$1@news.hal-pc.org>...
>What is the scope of "using namespace"?  The following compiles
>namespace Action
>{
>   enum Why {cause};
>};
>class How
>{
>public:
>   using namespace Action;  // point 1
>   int Foo(void);
>};
>int How::Foo(void)
>{
>   using namespace Action;  // point 2
>   return cause;
>}
>int main(void)
>{
>   How how;
>   int rc = how.Foo();
>   return rc?rc:cause;  // point 3
>}
Um, MSVC++6sp3, bcpp5.5, mingw2.95.2 - all of them reject this code,
specifically the lines "point 1" (syntax error at namespace)
and "point 3" (identifier cause undeclared).

I believe the following quote from the standard clarifies the situation:

[namespace.udir] 7.3.4 Using directive
"A /using-directive/ shall not appear in class scope, but may appear
in namespace scope or in block scope
...[snip]...
A /using-directive/ specifies that the names in the nominated namespace
can be used in the scope in which the /using-directive/ appears after
the /using-directive/".

Hope this help,
Constantine
=constim-at-SoftHome-dot-net=
"...as I said before, I never repeat myself..."


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: Fri, 13 Apr 2001 18:04:33 GMT
Raw View
"Constantine S. Timoshenkov" <constim@SoftHome.net.CUT_THIS> wrote in
message news:9b4k9e$1iqo$1@gavrilo.mtu.ru...
> Greg Brewer wrote in message <9b1uim$19su$1@news.hal-pc.org>...
> >namespace Action
> >{
> >   enum Why {cause};
> >};
> >class How
> >{
> >public:
> >   using namespace Action;  // point 1
> >   int Foo(void);
> >};
> Um, MSVC++6sp3, bcpp5.5, mingw2.95.2 - all of them reject this code,
> specifically the lines "point 1" (syntax error at namespace)
> and "point 3" (identifier cause undeclared).

Thanks.  bcpp 5.2 accepted the code; I was looking for it to reject "point
3".  I suspected there was an implementation problem but wasn't sure exactly
what the problem was.  I know that #define statements have unlimited scope;
however, I suspected that using statements were supposed to have limited
scope.  I wonder why the using-directive isn't allowed in the class scope?
I'm sure there is a good answer.  I once wondered why namespaces couldn't be
friends then realized that new functions can be added to a namespace at any
time.  Having a friend namespace would effectively disable a private
specification.

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.research.att.com/~austern/csc/faq.html                ]