Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2005, 05:05 AM
Andrew Ward
Guest
 
Posts: n/a
Default using namespace available outisde namespace

Hi,
The following code compiles fine for me, however I would have thought it
should return an error at least in foo(). Is there a good reason why the
using namespace N1; continues past the end of namespace N2?

namespace N1
{
class C1
{
public:
void f1() {}
};
}

namespace N2
{
using namespace N1;
class C2
{
C1 c;
void f2();
};
}

void N2::C2::f2()
{
C1 c;
c.f1();
}

void foo()
{
C1 c;
c.f1();
}
  #2  
Old July 25th, 2005, 05:25 AM
vindhya
Guest
 
Posts: n/a
Default Re: using namespace available outisde namespace

using namespace N1;

Because of this line. The scope of this doesnt ends and therefore you
are able to access C1 methods in void foo()

  #3  
Old July 25th, 2005, 05:25 AM
Andrew Ward
Guest
 
Posts: n/a
Default Re: using namespace available outisde namespace

vindhya wrote:[color=blue]
> using namespace N1;
>
> Because of this line. The scope of this doesnt ends and therefore you
> are able to access C1 methods in void foo()
>[/color]

I can see that is the way it works, though I was wondering if there is a
good reason for it to work this way. It seems like it would be better
for the 'using namespace N1' to finish at the end of the scope. Then you
could use this technique in header files so you don't have to keep on
typing N1::

  #4  
Old July 25th, 2005, 05:35 AM
vindhya
Guest
 
Posts: n/a
Default Re: using namespace available outisde namespace

In fact I was working with std namepace where I found that if I write
"using namespace std" in one header it was borrowed in almost 70% of
codes. So its better to use namespace A { } in the cases where there
may be any chances of ambiguity. In fact ISO standard also defines this
ambiguity.

  #5  
Old July 25th, 2005, 06:35 AM
Alan Johnson
Guest
 
Posts: n/a
Default Re: using namespace available outisde namespace

Andrew Ward wrote:[color=blue]
> Hi,
> The following code compiles fine for me, however I would have thought it
> should return an error at least in foo(). Is there a good reason why the
> using namespace N1; continues past the end of namespace N2?
>
> namespace N1
> {
> class C1
> {
> public:
> void f1() {}
> };
> }
>
> namespace N2
> {
> using namespace N1;
> class C2
> {
> C1 c;
> void f2();
> };
> }
>
> void N2::C2::f2()
> {
> C1 c;
> c.f1();
> }
>
> void foo()
> {
> C1 c;
> c.f1();
> }[/color]

The way I read the standard, the use of C1 in foo() is in fact an error.
For reference, g++ 4.0 (and maybe earlier versions, I haven't checked)
does consider it an error.

-Alan
  #6  
Old July 25th, 2005, 07:45 AM
Andrew Ward
Guest
 
Posts: n/a
Default Re: using namespace available outisde namespace

Alan Johnson wrote:[color=blue]
> Andrew Ward wrote:
>[color=green]
>> Hi,
>> The following code compiles fine for me, however I would have thought
>> it should return an error at least in foo(). Is there a good reason
>> why the using namespace N1; continues past the end of namespace N2?
>>
>> namespace N1
>> {
>> class C1
>> {
>> public:
>> void f1() {}
>> };
>> }
>>
>> namespace N2
>> {
>> using namespace N1;
>> class C2
>> {
>> C1 c;
>> void f2();
>> };
>> }
>>
>> void N2::C2::f2()
>> {
>> C1 c;
>> c.f1();
>> }
>>
>> void foo()
>> {
>> C1 c;
>> c.f1();
>> }[/color]
>
>
> The way I read the standard, the use of C1 in foo() is in fact an error.
> For reference, g++ 4.0 (and maybe earlier versions, I haven't checked)
> does consider it an error.
>
> -Alan[/color]

Well that would be nice to have, if only I didn't have to use VC7.1
I might ask the people over at ms.public.vc.language what the behaviour
is in VC8.0

Andrew.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles