ANSI C++ Namespaces

W

wschlanger

Hi, the following code behaves differently for GCC/MSC and Borland C++
5.6.

The question was, what order does symbol lookup happen in?

1. declared symbols, including using'd symbols.
2. parent namespace (nested)
3. using'd namespaces.

The above order is used by GCC and MSC, but Borland C++ 5.6 has (2)
and (3) flipped so that the following code is capable of detecting
your compiler:

What does ANSI C++ say about this?

Since it depends on compilers, shouldn't gcc be generating a warning?
Or does GCC have it right and is Borland C++ what's wrong?

If you could please run this program on an Intel C++ compiler or other
compilers, I am interested in the outcome. What does it say when you
run it on your compiler (besides GCC, MSC, and BCC?)

Thanks in advance,
Willow

---
#include <stdio.h>

namespace NSX
{
int a;
}

namespace NS1
{
int a;
namespace NS2
{
using namespace NSX;
// parent (nested) NS2 'and' using'd namespace NSX have a.
int &b = a;
}
}

int main()
{
if(&NS1::NS2::b == &NSX::a)
printf("BCC detected.\n");
else
if(&NS1::NS2::b == &NS1::a)
printf("GCC/MSC detected.\n");
else
printf("Error.\n");
return 0;
}
 
J

James Kanze

Hi, the following code behaves differently for GCC/MSC and Borland C++
5.6.
The question was, what order does symbol lookup happen in?
1. declared symbols, including using'd symbols.
2. parent namespace (nested)
3. using'd namespaces.
The above order is used by GCC and MSC, but Borland C++ 5.6 has (2)
and (3) flipped so that the following code is capable of detecting
your compiler:
What does ANSI C++ say about this?

The ISO C++ standard says: "During unqualified name lookup
(3.4.1), the names appear as if they were declared in the
nearest enclosing namespace which contains both the
using-directive and the nominated namespace." There's no
"order" specific to the namespace, it is "as if" the names were
declared in a specific namespace.
Since it depends on compilers, shouldn't gcc be generating a warning?
Or does GCC have it right and is Borland C++ what's wrong?

In your example, at least, I think that g++ has it right.

The issue is subtle enough, however, that I would avoid writing
such code. Even if the compiler gets it right, will it be clear
to the reader?
If you could please run this program on an Intel C++ compiler or other
compilers, I am interested in the outcome. What does it say when you
run it on your compiler (besides GCC, MSC, and BCC?)

Sun CC agrees with g++ here.
namespace NSX
{
int a;
}
namespace NS1
{
int a;
namespace NS2
{
using namespace NSX;
// parent (nested) NS2 'and' using'd namespace NSX have a.

This makes symbols in NSX available "as if" they had been
declared in global namespace (the nearest enclosing namespace
which contains both this using directive AND the nominated
namespace NSX). The symbol NS1::a should be found before the
global namespace is searched, so it will hide any symbol a made
available by the namespace directive above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top